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.
Related
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]
I am migrating data from one content management system to another. There is no relationship between old URLs and new URLs, although both contain query strings. I am trying to set up a set a rewrites that will redirect broad category lists of data from one to the other.
Here's a sample:
OLD
rss.php?categoryID=53
NEW
index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19
I tried
RewriteRule ^rss.php\?categoryID=53 index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
but it doesn't match. If I follow that one with
RewriteRule ^rss.php index.php?module=news&type=user&func=list&tid=1 [L]
if DOES match, so I conclude that the question mark in the old URL is causing the problem. I am already escaping the question mark. What do I do?
I will probably end up with about 50 of these in my .htaccess file.
You can't match against the query string (all that stuff after the ?) in a RewriteRule, you need to use a RewriteCond and match against the `%{QUERY_STRING} var:
RewriteCond %{QUERY_STRING} ^categoryID=53$
RewriteRule ^rss\.php$ /index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
Or change the brackets to [R=301,L] if you want to redirect the browser.
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.
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]....
I'm trying to remove query strings from my calendar, but my mod_rewrite is not appending the query string.
The website is http://cacrochester.com/Calendar
and if you click the link to go to a different month, the query string is usually http://cacrochester.com/Calendar?currentmonth=2010-11
With my rule below, it just doesn't append the query string so when you click the next month link, it just stays on the month October. What's wrong with my rule?
Here is my rule
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^.*$ http://cacrochester.com/Calendar? [NC,R=301,L]
EDIT:
What i want is to take a url like http://cacrochester.com/Calendar?currentmonth=2010-11 and turn it into something like http://cacrochester.com/Calendar/2010-11
You probably need your app to output relative urls like "/Calendar/2010-11". That's a simple code change.
Then in Apache you'd want to rewrite those urls, using:
RewriteRule ^/Calendar/([0-9]+-[0-9]{2})$ /Calendar.php?currentmonth=$1 [NC,QSA,L]
(You don't want a RewriteCond for this rule.)
Forcing a redirect with R=301 will only expose the internal url scheme. I don't think that's what you want.
To maintain query strings when rewriting, use the QSA (query string append) flag.
[NC,R=301,QSA,L]