简体   繁体   中英

.htaccess rewrite not working 301redirect

I have the following code in my .htaccess to rewrite the username.

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /profile.php?username=$1 [L]

</IfModule>

So this is the original url:

www.domain.com/profile.php?username='Mike' 

Into this:

www.domain.com/Mike

Now I have another rewrite rule for www 301 redirect. This also works great except when I'm at wwww.domain.com/Mike and I delete the 'www' and this is what I'm getting in my url:

www.domain.com/profile.php?username=Mike

My question is why im not getting the same url:(www.domain.com/Mike) after deleting 'www'?

Here is my full code in .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1 [L]

</IfModule>


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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]

Change order of your rules to keep 301 redirect rules before internal rewrite ones:

RewriteEngine On

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

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.domain.com/ [R=301,L]

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

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