Is my query string redirect right? - .htaccess

I want to redirect mypage.com/store/index.php?product_id=62&thisvar=doesntmatter to mypage.com/store#page-anchor. My current redirect looks like this, but doesn't seem to be doing anything. Am I doing something wrong?
RewriteCond %{QUERY_STRING} ^([^&]&)*product_id=62(&|$)
RewriteRule ^/store/index\.php$ /store#page-anchor [R=301,L]
Edit: I should note that I have the following lines before my rewrite rule:
RewriteEngine On
RewriteBase /

The ([^&]&)* bit doesn't look quite right. It matches strings like "a&b&c&" (a pair of a non-& character and a &, repeated any number of times), and that's probably not what you want. I guess you wanted to write ([^&]*&)*, but I'd suggest (^|&)product_id=62(&|$) (for safety, readability and elegance).
Oh and, if it's in your .htaccess (and not your httpd.conf), the pattern in the RewriteRule should be a relative URI, i.e. it shouldn't start with a /.

Related

mod_rewrite - check for string

I want to check if a URL contains the sting "-EN.htm", if so apply the rewrite.
That should be done with ^-EN.htm as follows, but the rule is not working:
RewriteCond %{REQUEST_URI} ^/(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm
RewriteRule ^(.*)$ /indexEN.php?folder=%1&follow=%2 [L]
What am I doing wrong?
Thank you for every help,
Scott
Your regular expression doesn't look right. You can also lose the condition and just move the pattern to the rewrite rule instead. Something along the lines of
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
You need to make the leading slash optional (in htaccess this is stripped off) and instead of using % backreferences, use the $ ones.
Now on to your pattern, it's not valid. The ^ matches the beginning of the string (the URI), so if you have two of them and you're not trying to literally match the ^ character (which you'd need to escape), then the expression will never match anything. Without any examples of URLs that you're having to deal with, I assume you probably just want to ditch the second ^:
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)-EN.htm /indexEN.php?folder=$1&follow=$2 [L]

htaccess codes behaves differently when written in single line and when written in two different lines

I was trying to learn the htaccess basics but initial step made me go crazy and lost some precious time..
htaccess codes behaves differently when written in single line and when written in two different lines
the code below
RewriteEngine on
RewriteRule .* good.html
And the code below
RewriteEngine on RewriteRule .* good.html
Behave differently..
Please explain how it goes..
I guess, its because htaccess checks all the conditions first and then works..
but that too should not make it work like that..
Apache directives expect to be on a line by itself. So when you have:
RewriteEngine on RewriteRule .* good.html
The first "word" is the directive, e.g. RewriteEngine. It looks for whether it should be on or off, and the next "word" is on. So far so good, the rewrite engine is turned on. But there's a bunch of other crap after that and it'll be ignored.
But when you have:
RewriteEngine on
RewriteRule .* good.html
The rewrite engine is turned on like normal. Then a RewriteRule directive is processed. The first "word" in the line is RewriteRule, so the params are RewriteRule regex target [flags].
So when they're on different lines, everything gets internally rewritten to good.html. On the same time, everything after on is ignored.

htaccess redirect with php variables ...

I'm using this format in my htaccess to redirect several pages/links:
RewriteEngine On
RewriteRule special.php http://www.mysite.com [R=301]
...
...
RewriteRule http://www.mysite.com/special.php?t=master http://www.mysite.com/index.php?q=former [R=301, L]
I noticed, first, that only the top line is catching anything, and in fact the others, like the bottom line, did nothing until I put in that top line. Any ideas why?
Second, mysite.com/special.php?t=grave is redirected, by the above top line, to mysite.com/?t=grave , thus retaining the variables in the URL. I don't want this, I simply want it to go to mysite.com with no variables. How do I do this?
Thanks,
Derek
First, your first rule catches any URI with special.php in it, even if it is followed by a bunch of characters. To limit it to only and exactly special.php, and to make sure the query string is discarded, change it to
RewriteRule ^special.php$ http://www.mysite.com/? [L, R=301]
Secondly, rewrite rules only match the part after http://www.mysite.com/ (note the last slash) and before the query string (the part after the question mark). So if you change the format of those rules to
RewriteCond %{QUERY_STRING} t=master
RewriteRule ^special.php$ index.php?q=former [R=301, L]
you should be good to go.

mod_rewrite doesn't actually... re-write the URL

Basically, I've been trying to make some friendly URL's via .htaccess using mod_rewrite - and I've managed to get it to work... but only with basic stuff like:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php http://www.google.co.uk [L]
So mod_rewrite works, and I can re-direct to other sites, other files/directories in my server, etc. - but it seems to not work when I use this:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php?user=$1 ^profile/user/([^/]*)/$ [L]
Any help on this would be great, as I pretty much suck at mod_rewrite, but it's something I need to learn.
Cheers!
Change your [L] to [R,L] to force an actual HTTP redirect. Otherwise it just does the rewriting internally (when possible), which only affects the mapping from the URI to the filesystem. (See the description of the [R] flag at http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteflags.)
Wrong.
## rewriting from to
RewriteRule ^profile.php?user=$1 ^profile/user/([^/]*)/$ [L]
Should be
## rewriting from to
RewriteRule ^profile/user/([^/]+)$ profile.php?user=$1 [L]
Your configuration currently is this:
RewriteEngine On
RewriteBase /
RewriteRule ^profile.php?user=$1 ^profile/user/([^/]*)/$ [L]
In the RewriteRule you swapped the from and to parameters.
Assuming that on your server there is a directory structure like this:
/var/www/htdocs/profile/user/albert
/var/www/htdocs/profile/user/bob
Then you can use the following rule:
RewriteCond ${QUERY_STRING} ^user=(\w+)$
RewriteRule ^profile\.php$ profile/user/%1 [L]
There are some points that you got wrong here:
The request to "/profile.php?user=bob" first gets split into the Request URI and the Query String. Only the Request URI will be used by mod_rewrite. Therefore you have to handle the query string separately.
I restricted the user name to only [A-Za-z0-9_]. If I had allowed all characters, an attacker could easily call /profile.php?user=../../config.php, which would be rewritten to profile/user/../../config.php, and you probably don't want to share that file with the world.
The arguments to the RewriteRule directive are completely different regarding their syntax.
The first argument (the from part) is a regular expression, which usually starts with a caret ^ and ends with a dollar $.
The second argument (the to part) is the replacement, which is almost only a simple string, with only some special features. This string usually doesn't start with a caret, but looks rather like a pathname.

optional second directory in htaccess redirect

Hi
im wanting to write a mod redirect which handles the following:
www.domain.co.uk/brands
rewrites to www.domain.co.uk/index.php?p=brands
www.domain.co.uk/brands/5
rewrites to www.domain.co.uk/index.php?p=brands&go=5
Can this be achieved in one line without a conditional statement?
I have written this, but the second line is ignored:
RewriteRule ^(.*)\.html$ index.php\?p=$1 [L]
RewriteRule ^((.*)/*)(.*)\.html$ index.php?p=$1&go=$2 [L]
Any help would be much appreciated
Well, it seems to me that the first rule would match both versions, and rewrite www.domain.co.uk/brands/5 to www.domain.co.uk/index.php?p=brands/5 -- and then the [L] flag makes the matching stop. It wouldn't match the second rule after the rewrite, anyway.
The second regexp has an asterisk too many (after the slash) and one set of parens too many as well, but if you fix that and move it above the other one, it might help.
Just reverse the order. As a general practice you must have most specific rules first and most generic as last. Try this in your .htaccess:
RewriteRule ^([^/]*)/(.*)\.html$ /index.php?p=$1&go=$2 [L,NC,QSA]
RewriteRule ^(.*)\.html$ /index.php?p=$1 [L,NC,QSA]
This will redirect URI of '/brands/5.html' to /index.php?p=brands&go=5 and a URI of '/brands.html' to /index.php?p=brands
RewriteRule (.*)(/(.*))?$ index.php?p=$1&go=$3 [L]
should do it.
The reason your second rule fails is, you are matching one character (.) followed by a / at the start of the string, so immediately any urls with more than one character before the first slash will fail. You are also insisting that the url ends with html but in your examples they don't. Also for the future, remember . matches any single character so you probably meant to escape the . before html.

Resources