Ryan Hipkiss Ryan Hipkiss - 1 month ago 8
Apache Configuration Question

.htaccess Redirect to a URL that uses a rewrite rule

I have Redirect like so:

Redirect 301 /url-1 /url-2


However,
/url-2
matches a rewrite like such:

RewriteRule ^([^.]+)$ index.php?view=$1 [QSA,L]


This is redirecting
/url-1
to:

/url-2?view=url-1


How can I get this to redirect and then rewrite correctly?

Answer

Few suggestions:

  1. Don't mix Redirect directive with mod_rewrite rules
  2. Keep redirect rules before rewrite rules.
  3. Skip files and directories from last rule

You can use these rules:

RewriteEngine On

RewriteRule ^url-1/? /url-2 [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+?)/?$ index.php?view=$1 [QSA,L]

Remember to clear your browser cache before testing this.