In trying to get my head around mod_rewrite, I've added a simple rewrite rule:
RewriteRule ^z$ z1.html
The idea is to let someone access www.mysite.com/z and have them get www.mysite.com/z1.html
But this doesn't work. I have to change it to
RewriteRule ^/z$ /z1.html
But I'm not seeing anything in the mod_rewrite rules that requires a "/" in front of the terms, so why doesn't the first one work?
A bit more complicated, I have the rule
RewriteRule ^/([^.]+)$ /1$.html
and this one doesn't work either, even with the "/" characters.
The idea here is to let some enter www.mywebsite.com/z1 and have it become www.mywebsite.com/z1.html.
Does anyone see the problem?
Thanks.
If you have mod rewrite rules in vhost/server config, it requires a / as part of the pattern. The second rule doesn't work because you want $1, and not 1$:
RewriteRule ^/([^.]+)$ /$1.html [L]
Related
I'm trying to transform "domain.com/index.php?site=food&category=beef" into "domain.com/food/beef" but it does not work, no matter what I try. It always leaves the original domain and I get no errors.
I think it's my fault, I tried this for 3 different URLs on 3 different servers (and 3 different projects)... it just seems like I don't get how mod_rewrite really works, though I read every documentation on this topic I found. I even spent days here on SO without finding any solution.
Mod_rewrite is enabled on the server:
RewriteEngine On
RewriteRule ^ http://www.google.com [R,L]
gives me "http://www.google.com/?site=food&category=beef". It looks like mod_rewrite does not recognise the query string... So I tried several solutions with RewriteCond %{QUERY_STRING}... but nothing works :/
Hopefully you guys can help me! I'm going insane on this!
Thanks in advance!
Try:
RewriteEngine on
RewriteRule ^food/beef$ index.php?site=food&category=beef [L]
Or more generally:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?site=$1&category=$2 [L]
Are you trying to do something like this?
RewriteRule ^([^/]+)/([^/]+)/? /index.php?site=$1&category=$2 [L]
This will make it so when you go to http://domain.com/food/beef the request gets rewritten to "/index.php?site=food&category=beef" internally and index.php is used to serve the original request. The browser's location bar will still say "http://domain.com/food/beef".
If you want the location bar to say http://domain.com/index.php?site=food&category=beef then add an "R," to the "[L]". If this is backwards and you want it so when someone enters http://domain.com/index.php?site=food&category=beef in the location bar, and the request gets rewritten to "/food/beef" internally on the server, then you need to parse out the query string using RewriteCond:
RewriteCond %{QUERY_STRING} ^site=([^&]+)&category=([^&]+)
RewriteRule ^index.php /%1/%2? [L]
The same thing applies with the "R" causing a browser redirect like the first example. If you want the location bar to change to http://domain.com/food/beef then the brackets should look like: [L,R]. Note that you need a ? at the end of the target in the rule, so that query strings don't get thrown in. That is why in your google example, the query string is being appended.
EDIT:
Seeing as you just wanted to change what's in the browser's location bar and not where the content is:
You need to re-rewrite what the 2nd rule above has rewritten BACK to index.php, but without a redirect. In order to keep the 2 rules from looping indefinitely because one rule rewrites to the other rule and vice versa, you need to add a flag somewhere to keep the 2nd rule above from redirecting you over and over again.
So combining the two, you'll have this:
RewriteRule ^([^/]+)/([^/]+)/? /index.php?site=$1&category=$2&redirected [L]
RewriteCond %{QUERY_STRING} !redirected
RewriteCond %{QUERY_STRING} ^site=([^&]+)&category=([^&]+)
RewriteRule ^index.php /%1/%2? [L,R=301]
Note the redirected parameter in the query string. This gets inserted when someone tries to access the clean version of the url, e.g. "/food/beef". internally, it gets rerouted to index.php but since the rule doesn't have a "R", the browser's location bar doesn't change.
The second rule now checks if the request contains the redirected param in the query string. If it doesn't, that means someone entered in their browser's location bar the index.php url, so redirect the browser to the clean version.
My .htaccess is as follows:
RewriteEngine On
RewriteRule ^client/([0-9a-zA-Z]+)/(.*)$ licensee/client/$2?id=$1 [L,QSA]
RewriteRule ^licensee/(.*)/$ licensee/$2?id=$1 [L,QSA]
If I visit /client/asdf/ in my browser however, it uses the second rewrite rule instead of the first. If I comment out the second rewrite rule, it uses the first rule as expected.
I can't figure out how the second rule could possibly match /client/asdf/ - have I overlooked something?
When you visit /client/asdf/, it matches the first RewriteRule, so a new request becomes "licensee/client/?id=asdf" which matches your second rule.
How can I remove main/ in http://domain/main/about
so the URL will be like this http://domain/about
Thanks
Well, in the absence of any further details, you could try something like this:
RewriteRule (.*)/main/(.*) $1/$2
But this is a VERY broad rule - you'd probably want to tune it more tightly than that. Try turning the RewriteLogLevel to 9 and watching the rules as they get processed so you can get a better idea of what is being matched.
If you only need one file to be rewritten :
RewriteRule main/about about [L]
or else the TML answer is good :
RewriteRule (.*)/main/(.*) $1/$2 [L]
It rewrites any URL with a /main (even in third, fourth, ... places) in it to prefix/suffix URL.
i have tried it all!
this:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
works only if i don't put the http at the beggining
how do make that to work:
if there is http redirect to https
if there is www redirect to non-www
and ofcourse both on the same time
http://www.domain.com -> https://domain.com
www.domain.com --> https://domain.com
http://domain.com --> https://domain.com
with every subfolders after and query!
I assume you also want
https://www.domain.com -> https://domain.com
Did you ever get this working? I'm having trouble getting a test https site going to double-check this.
In the meantime, I do see a couple things, so try this instead (this assumes isapi_rewrite v3, which it looks like you're using):
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*)$ https://%1$1 [NC,R=301]
This adds parentheses to the RewriteRule to capture the url for the $1.
Then the slash between the %1$1 isn't needed, since there's one at the start of the $1 capture.
I like to use NC for not case-sensitive, and the R rule is a final rule, so you don't need the L for last.
EDIT
I revisited this answer, to update/clarify a couple of secondary issues in my original answer above. These don't change the main solution above, which was to add the parentheses to do the capture.
Original:
Then the slash between the %1$1 isn't needed, since there's one at the start of the $1 capture.
This actually depends where the rules are, and whether there's a RewriteBase statement. Common shared-host configurations don't have the leading slash here in the rules, so the slash would be needed. If you're not sure, you can try with and without.
Original:
I like to use NC for not case-sensitive, and the R rule is a final rule, so you don't need the L for last.
It turns out it is useful to have L with R for performance, as [NC,R=301,L]. I had actually asked this at Helicon Tech a year before this question, but had forgetten it:
http://www.helicontech.com/forum/14826-Does_Redirect_need_Last.html
From their response:
... the reason for us to use [L] in 301 redirect rules is that redirect occurs not that
immediately. Even though the rule is matched futher rules will be proccessed (run through), unless you have [L]....
In ISAPI_rewrite 3 documentation is this example
RewriteRule ^(.*?\.asp)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,LP,QSA]
http://www.example.com/foo.asp/a/A/b/B/c/C
http://www.example.org/foo.asp?a=A&b=B&c=C
Since my products can have more or less parameters in query string, this seemed like right approach. Based on given example I tried to create a rule for my case with no luck.
http://www.example.com/product-name.aspx/a/A/b/B/c/C
http://www.example.org/products.aspx?a=A&b=B&c=C
After more digging I came up with solution. Seems like there is no other way but using two rules.
RewriteRule ^(.*?\.htm)/([^/]*)/([^/]*)(.+)? $1$4?$2=$3 [NC,LP,QSA]
RewriteRule ^.*?\.htm$ product.aspx [NC,L]