Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top