URL重写网站主题并同时删除www

时间:2014-01-13 02:41:09

标签: php regex apache .htaccess mod-rewrite

我正在尝试执行网址重写以完成以下操作:

www.website.com - > website.com

和链接页面

www.website.com/links.php?subject=Awesomeness - > website.com/Awesomeness

有一次我确实让最后一部分工作了,但添加www会让它绊倒。我目前的htaccess重写区域如下:

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    # Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)$ links.php?subject=$1 [NC,L]
</IfModule>

<IfModule mod_rewrite.c>
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>

1 个答案:

答案 0 :(得分:0)

在重写之前执行301规则。试试这段代码:

DirectoryIndex index.php
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ links.php?subject=$1 [QSA,L]