I have issue with RewriteRule in .htaccess. It works if I do this:
RewriteRule example1.com/test example2.com/test1#tab1 [L,NE,R=301]
But when I try to use hash in first URL it doesn't:
RewriteRule example1.com/test#tab0 example2.com/test1#tab1 [L,NE,R=301]
You can't match fragments in .htaccess. The fragment is never sent to the server.
You can match example2.com/test1?tab0:
https://stackoverflow.com/a/20383730/3150271
Related
I'm trying to do a .htaccess redirect with a parameter but it's not working. Seems like my regex are also wrong. :(
Original URL 1: http://www.example.com/?team=john-doe
Original URL 2: http://www.example.com/?team
Target URL: http://www.example.com/company/
I tried:
RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]
Any help would be much appreciated.
Found a generator that works perfectly:
https://donatstudios.com/RewriteRule_Generator
# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]
Your RewriteRule is malformed, you are missing a pattern (first argument). Try something like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]
The RewriteRule pattern ^$ matches requests for the domain root. The ? on the end of the RewriteRule substitution strips the query string from the redirected URL (on Apache 2.4+ you can use the QSD flag instead).
The CondPattern ^team(?:=john-doe)? matches either "team=john-doe" (URL#1) or "team" (URL#2) at the start of the query string. The (?: part just makes it non-capturing.
You will need to clear your browser cache before testing.
RewriteRule ^id/([^/]*)$ /product.cgi?id=$1 [L]
The above will execute example.com/product.cgi?id=* when url example.com/id/* is requested by the user. I would like the other way around,
i want to display to the user example.com/id/* when example.com/product.cgi?id=* is requested. Keeping in mind that example.com/id/* does not exist on the server, it would work line an alias. Is it even possible?
You can use this in your .htaccess to do the reverse.
RewriteEngine On
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /product\.cgi\?id=([^&\ ]+)
RewriteRule ^ /id/%1? [R=301,L]
RewriteRule ^id/([^/]*)$ /product.cgi?id=$1 [L]
I have a website that is getting a lot of requests like this:
http://site/folder/url.html&sa=U&ei=c9hNU7aVDOy(and more gibberish)
I've added this to my .htaccess:
RewriteRule ^(.*)&sa=U /$1 [L,NC,R=301]
But it doesn't work: the site still hands out 404 pages. (I've flushed the caches).
Help? Many thanks!
Usually, it would have been fair to use QUERY_STRING to match query string parameters.
But since you don't have leading ? it does not consider it as query string.
Your rule seems correct.
Anyway, it looks like it does not work as expected.
You can try the following alternative, which is working
RewriteRule ^(.*)&sa=U.*$ /$1 [L,NC,R=301]
EDIT: your rule is working the way you want, you only have to make your pattern more specific
RewriteRule ^([^&]*)&sa=U /$1 [L,NC,R=301]
If you want to make an rule base on the query string (= GET parameters), you have to use a RewriteCond, you can't access them through a RewriteRule. So :
RewriteCond %{QUERY_STRING} sa=U [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
I have a two part problem that I have only been successful with the first part.
I have the following listed in .htaccess which works great:
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
So visitors can go straight to mysite.com/senior and the correct internal page (demo.php?dID=1) gets pulled up.
My problem is that I also would like a rewrite where /demo.php?dID=1 shows up in the URL bar as /senior. So existing links show up with the new user friendly url.
My attempt so far has been:
RewriteCond %{QUERY_STRING} ^dID=1$
RewriteRule ^demo.php$ senior [NC]
RewriteRule ^senior/?$ demo.php?dID=1 [NC,L]
Thanks for your time and help.
You want to match against the request instead of the query string and redirect the browser:
RewriteCond %{THE_REQUEST} \ /demo\.php\?dID=1($|\ |&)
RewriteRule ^demo.php$ /senior? [NC,R]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Place this additional rule before your current rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+demo\.php\?dID=([^\s&]+) [NC]
RewriteRule ^ /senior? [R=302,L]
RewriteRule ^senior/?$ /demo.php?dID=1 [NC,L]
Your current rule based on QUERY_STRING will loop since internal rewrite rule will populate the QUERY_STRING and both rule will keep triggering each other.
So here's what I have.
www.website.com/foo (pretty URL to use on marketing pieces)
www.website.com/foobar (URL that actually exists on site)
I can get www.website.com/foo working perfectly with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /foo [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
But that makes the www.website.com/foobar URL go there as well.
I'm sure this is a regex issue and I just don't know the correct symbol to get things working properly, but how can I make /foo redirect properly without effecting /foobar ?
Thanks.
Try this instead:
RewriteCond %{REQUEST_URI} ^/foo$ [NC]
RewriteRule ^ http://www.website.com/redirected/url-goes-here/ [L,R=301]
REQUEST_URI will get rid of the extra request headers that THE_REQUEST has. Then you can match the beginning and end of the requested URL with ^ and $.
You don't need the RewriteCond. Just be specific with the RewriteRule pattern
RewriteRule ^foo$ http://www.website.com/redirected/url-goes-here/ [L,R]
See more about regular expression.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.