htaccess redirect and rewrite rules conflicting


htaccess redirect and rewrite rules conflicting
I want to rewrite all requests to index.php?r=
So that /sth becomes /index.php?r=sth
I have applied this rule that works:
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^.*.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]
However I want to also have redirects such as
Redirect 301 /sth /sth-new
What happens is that it works but the url becomes:
/sth-new?r=sth
Do you have any solutions - suggestions about why this is happening?
How can I implement a global rule and also have simple redirects?
1 Answer
1
You can do it like this:
RewriteRule ^sth/?$ /sth-new? [L,R=301]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]
You should keep external redirect rules before your internal rewrite rules.
Thank you this works. I also have some external links that are like sth/?someparam=sth. This should redirect to /sth-new (no params). How can I implement this using the above rule?
– Stathis G.
May 12 '14 at 7:41
sure, check updated answer.
– anubhava
May 12 '14 at 8:11
Nope it didnt' work... This is the exact example: #RewriteRule ^accident_service/?tab=1/?$ /accident-service/antikatastash-oxhmatos [R=301,L]
– Stathis G.
May 12 '14 at 8:59
That is different URL from your earlier comment where you wanted to redirect
sth/?someparam=sth
to /sth-new?
– anubhava
May 12 '14 at 9:27
sth/?someparam=sth
/sth-new?
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You're welcome, glad it worked out.
– anubhava
May 12 '14 at 7:31