htaccess rewrite losing $_GET on second rewrite? - .htaccess

Here are my two rewrites:
RewriteRule folder/(.*)/$ /folder/subfolder/index.php?s=$1
RewriteRule folder/(.*)/review/$ /folder/subfolder/review.php?s=$1
The first rewrite works perfectly, for example:
http://www.site.com/folder/hello/
But the second:
http://www.site.com/folder/hello/review/
It doesn't pass the "hello" as the $_GET over.
Why is the second rewrite losing its parameter?

Because it's reading (.*) to include "...site.com/folder/hello/review/" which then doesn't match any rewrite rule.
Change the rewrite rule to:
RewriteRule folder/([0-9a-zA-Z-]+)/review/$ /folder/subfolder/review.php?s=$1
That will limit it to alpha-numeric and "-" characters only.

Related

.htaccess rewrite rule, change URL

I'm looking for the following to change the URL with two variables in a .htaccess file, from:
domain.com/folder/a123/url/index.php
domain.com/otherfolder/a321/url/index.php
to:
domain.com/folder/url/index.php
domain.com/otherfolder/url/index.php
I would prefer it in a single line .htaccess RewriteRule, if possible.
"folder" and "otherfolder" are the only two "folder names" and "a123"/"a321" can be anything starting with "a" and ending with random numbers.
You can do that in one with just one rule using this:
RewriteEngine on
RewriteRule ^(.+)/a[0-9]*/url/index.php$ /$1/url/index.php [R=301,NC]
But if you want to redirect all pages and not only index.php then use this:
RewriteRule ^(.+)/a[0-9]*/url/(.*)$ /$1/url/$2 [R=301,NC]
Also, if you want to rewrite the URLs instead of redirect them change [R=301,NC] to [NC].
Note that if you have that pattern in other URLs this rule will also apply, for example it will also redirect domain.com/adifferentfolder/a567/url/index.php to domain.com/adifferentfolder/url/index.php, if you have those type URLs and don't want to redirect them you need to use two rules instead:
RewriteRule ^folder/a[0-9]*/url/(.*)$ /folder/url/$1 [R=301,NC]
RewriteRule ^otherfolder/a[0-9]*/url/(.*)$ /otherfolder/url/$1 [R=301,NC]

htaccess rewrite - removing characters from url

Looking to rewrite a part of a URL and stuck with dealing with special characters.
original
http://www.testwebsite.com/Products/Apple/*/!Accessories
desired result
http://www.testwebsite.com/Products/Apple-Accessories
I would also like to redirect (can a word be removed?) testwebsite.com/Products/Dell-Laptop/*/!Accessories
to testwebsite.com/Products/Dell-Accessories
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^(Products/[^/]+)/\*/!(.+)$ /$1-$2 [R=301,L,NE]

Mod rewrite to redirect except on a certain page

Trying to write a rewrite rule in my htaccess so that any request to /en/shop/index.html or /fr/shop/index.html stays on the server, but if the user goes to any other page it redirects to a different server. Here's what I've got so far and it doesn't work.
RewriteRule ^(.*)/shop/(.*) [L]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [R=301]
Add a dash to tell the first RewriteRule that you want the matches to be passed through unchanged:
RewriteRule ^.*/shop(/.*)?$ - [L]
I also removed the first set of parentheses since you're not using the results of the match so there's no need to store the matched patterns. I assumed you might need to match /shop without a trailing slash so that's why the (/.*)? section is there.

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

htaccess rewrite ONLY if first part of path is numeric

Is there an htaccess rule that will only rewrite if the first part of a path is numeric, so that http://www.example.com/123/whatever hits the rewrite rule, but http://www.example.com/user/whatever does not?
Here is a rewrite rule for my little site I am building
RewriteEngine on
RewriteRule ([a-zA-Z])/ index.php?k=$1
RewriteRule ([0-9]+)/ index.php?id=$1
So you can see that the regex rule [0-9]+ will match any numbers successively. The [a-zA-Z] will match letters.
You can match numbers in your pattern. For example:
RewriteRule ^([0-9]+)/(.*) /foo/$2?bar=$1
Will rewrite http://www.example.com/123/whatever to http://www.example.com/foo/whatever?bar=123 but leave /user/whatever alone.

Resources