将www添加到url并从url中删除.php

时间:2014-04-06 22:15:44

标签: .htaccess

所以我需要网址看起来像这样

www.mydomain.com/about

这是我目前的HTACCESS代码

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

# hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

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

# To add www header
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ www.mydomain.com/$1 [L,R=301]


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


#404 redirect
ErrorDocument 404 http://www.mydomain.com/

我认为问题出现在删除php区域。我认为它删除.php并将网址重写为mydomain.com/about,同时"添加www标题"规则是强制www。如何修复?

1 个答案:

答案 0 :(得分:1)

Options +MultiViews

# Turn mod_rewrite on
  RewriteEngine On
  RewriteBase /

# To add www header
  RewriteCond %{HTTP_HOST} ^mydomain.com$
  RewriteRule ^(.*) http://www.mydomain.com/$1  [QSA,L,R=301]

# To Remove .php-extension from url
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteCond %{REQUEST_URI} ^(.*)\.php$
  RewriteRule . %1 [R=301,L]