PlayerWet PlayerWet - 2 months ago 9
Apache Configuration Question

301 redirect apache shows wrong url

it turns out that for different reasons have had to change my website URL, the url old are indexed in Google and I want you to go through them, the new content is displayed with the new url.

I added this:

Redirect 301 /library/books/pitz-lower.php http://www.example.com/library/pitz-lower.php


But to get from google, the url in the browser looks like this:

http://www.example.com/library/pitz-lower.php?url=library/pitz-lower


htaccess

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On


RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]




RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /library/library\.php\?url=([^\s]+) [NC]
RewriteRule ^ /library/%1.php? [L,R=301]
RewriteCond %{REQUEST_URI} !^/library/library\.php
RewriteRule ^library/(.+)\.php$ /library/library.php?url=$1 [L]

</IfModule>

#r301

Redirect 301 /library/books/pitz-lower.php http://www.example.com/library/pitz-lower.php


I do not understand why this happens, someone gives me a hand? Thank you !!

Answer

There are 2 issues:

  1. Mixing mod_rewrite rules and mod_alias rules (i.e. Redirect directive) is not good as these 2 modules run at different times.
  2. Keep 301 redirect rules before internal rewrite ones.

Your modified .htaccess should be:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^library/books/pitz-lower\.php$ /library/pitz-lower.php [L,NC,R=301]

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /library/library\.php\?url=([^\s]+) [NC]
RewriteRule ^ /library/%1.php? [L,R=301]

RewriteCond %{REQUEST_URI} !^/library/library\.php
RewriteRule ^library/(.+)\.php$ /library/library.php?url=$1 [L,QSA]

Remember to clear your browser cache while testing this.

Comments