简体   繁体   中英

301 redirect in htaccess file

I am facing problems when creating a 301 redirect on my site. When I am creating a redirect the browser is showing the full qherysting rather than directing the page.

eg. I am wanting to direct /contact to /contact-us

What I am getting is:

/contact-us?page=contact

Here is the code from my .htaccess:

RewriteEngine on

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

RewriteBase /

redirect 301 /contact /contact-us 
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

Any ideas why this is happening?

This is a mod_alias/mod_rewrite conflict. The path processing pipeline is handling the 2 things separately. You can just stick with mod_rewrite and replace

redirect 301 /contact /contact-us 

with

RewriteRule ^contact$ /contact-us [L,R=301]

So the rewriting will stop once it gets here and the last rule never gets applied. Otherwise, mod_alias will redirect the URL, but not until it's run the URI through mod_rewrite, thus you get this globbed together redirect. If you don't mind a trailing ? , you could also add it to the end of your redirect target:

redirect 301 /contact /contact-us?

so that the query string won't get appended.

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI}


RewriteRule ^contact[/]?$ /contact-us [R=301]
RewriteRule ^([^/\.]+)[/]?$ /index.php?page=$1 [L]

Do it this way:

Redirect 301 /contact http://www.domain.com/contact-us

Use the full url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM