dhath dhath - 1 year ago 65
Apache Configuration Question

301 redirects are adding ?page=

I've set up some 301 redirects in my .htaccess file... for files that don't exist. They are indeed redirecting but with an added query. For example:

Using one of my redirects on my htaccess file below:
if I type in example.com/about-example.php it should redirect me to example.com/about-example.html and it kind of is but it takes me to example.com/about-example.html?page=about-example.php with this added ?page=(redirect) and moz is seeing that redirected page with a query at the end as a duplicate to the original page.

Here is what my htaccess looks like... (changing a lot to example/xyz)

Options -Indexes +FollowSymLinks

RewriteEngine on
RewriteBase /

# Remove "www."
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# Redirect any requests for html files to index
RewriteRule ^(.+)\.html index.php?page=$1 [L]

# Rewrite any request for subdirectories to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]

RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*example\.net/ [NC,OR]


# 6 month for most static assets
<filesMatch ".(jpg|jpeg|png|gif|gs|ico)$">
Header set Cache-Control "max-age=1576800, public"

# 1 month for most static assets
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=2628000, public"

<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 month"

ExpiresByType text/css "access plus 1 year"

# Data interchange
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"

# Favicon (cannot be renamed!)
ExpiresByType image/x-icon "access plus 1 week"

# HTML components (HTCs)
ExpiresByType text/x-component "access plus 1 month"

ExpiresByType text/html "access plus 0 seconds"

# JavaScript
ExpiresByType application/javascript "access plus 1 year"

# Manifest files
ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
ExpiresByType text/cache-manifest "access plus 0 seconds"

# Media
ExpiresByType audio/ogg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/ogg "access plus 1 month"
ExpiresByType video/webm "access plus 1 month"

# Web feeds
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"

# Web fonts
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"

Redirect 301 /about-example.php http://example.com/about-example.html
Redirect 301 /vacation-faq.php http://example.com/faqs.html
Redirect 301 /3d-tour.html http://example.com/plans.html
Redirect 301 /images/cool/04.jpg http://example.com/gallery.html

Am I missing a command, or are the redirects in the wrong place? I have tried rewrite rules, redirectmatch... and a bunch of other things with either the same result or 500 internal errors.. (edit) I have also messed with the placement of the redirects to no avail..

Answer Source

You need to update your rule and add a ? at the end. Try these rules instead.

Options -MultiViews
RewriteEngine on
#if request is a real file or dir do nothing
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

#else do these redirects
RewriteRule ^about-example\.php http://example.com/about-example.html? [R=301,L]
RewriteRule ^vacation-faq\.php http://example.com/faqs.html? [R=301,L]
RewriteRule ^3d-tour\.html http://example.com/plans.html? [R=301,L]
RewriteRule ^images/cool/04\.jpg http://example.com/gallery.html? [R=301,L]

Let me know how this answer works for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download