将www重定向到非www作品适用于除root之外的所有URL

时间:2012-09-18 05:44:35

标签: wordpress apache .htaccess mod-rewrite

我的Apache htaccess文件中有以下代码,用于将所有www URL重定向到wordpress上的非www URL。这适用于除root之外的所有URL。

例如,http://www.noshon.it/recipes正确地重定向到http://noshon.it/recipes。但是,http://www.noshon.it错误地重定向到http://noshon.it/wordpress(收到404页面)。

我无法弄清楚为什么所有ULR都可以工作,除了root。任何帮助将不胜感激。

RewriteEngine On   
RewriteCond %{HTTP_HOST} ^www\.noshon\.it$ [NC]  
RewriteRule ^(.*)$ http://noshon.it/$1 [R=301,L]  
RewriteBase /  
RewriteRule ^index\.php$ - [L]  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . /index.php [L]  

1 个答案:

答案 0 :(得分:0)

感谢@WebChemist建议重定向发生在另一个文件中。我在Bitnami EC2实例的/ opt / bitnami / apps / wordpress / conf /目录中找到了一个名为wordpress.conf的文件。该文件包含以下代码:

# App url moved to root
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
#Alias /wordpress/ "/opt/bitnami/apps/wordpress/htdocs/"
#Alias /wordpress "/opt/bitnami/apps/wordpress/htdocs"

<Directory "/opt/bitnami/apps/wordpress/htdocs">
    Options Indexes MultiViews +FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
<//Directory>

# Uncomment the following lines to see your application in the root
# of your URL. This is not compatible with more than one application.
RewriteEngine On
RewriteRule ^/$ /wordpress/ [PT]

重写发生在此文件的最后一行:

RewriteRule ^/$ /wordpress/ [PT]  

我将此文件修改为以下内容,重定向现在正在运行:

# App url moved to root
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs"
#Alias /wordpress/ "/opt/bitnami/apps/wordpress/htdocs/"
#Alias /wordpress "/opt/bitnami/apps/wordpress/htdocs"

<Directory "/opt/bitnami/apps/wordpress/htdocs">
    Options Indexes MultiViews +FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    RewriteEngine On
    #RewriteBase /wordpress/
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php [L]
<//Directory>

# Uncomment the following lines to see your application in the root
# of your URL. This is not compatible with more than one application.
#RewriteEngine On
#RewriteRule ^/$ /wordpress/ [PT]