Apache htaccess重写index.php,如果没有www,则重定向

Apache htaccess重写index.php,如果没有www,则重定向,apache,.htaccess,mod-rewrite,seo,url-rewriting,Apache,.htaccess,Mod Rewrite,Seo,Url Rewriting,我的网站url是www.site.com 如果用户访问=site.com,我需要在301中重定向到www.site.com 如果用户访问=www.site.com/index.php/asd,我需要在301中重定向到www.site.com/asd 如果用户访问=site.com/asd,我需要在301中重定向到www.site.com/asd 从.htaccess文件中是否可以实现所有这些条件?有人能帮我吗,伙计们?:) 谢谢;) 在.htaccess文件中尝试以下规则: Options +F

我的网站url是www.site.com

如果用户访问=site.com,我需要在301中重定向到www.site.com

如果用户访问=www.site.com/index.php/asd,我需要在301中重定向到www.site.com/asd

如果用户访问=site.com/asd,我需要在301中重定向到www.site.com/asd

从.htaccess文件中是否可以实现所有这些条件?有人能帮我吗,伙计们?:)


谢谢;)

在.htaccess文件中尝试以下规则:

Options +FollowSymlinks -MultiViews
RewriteEngine on

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

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

我的意思是,代码将我从site.com重定向到www.site.com,但如果我转到site.com/asd,它将我重定向到www.site.com/index.php/asd,而我希望被重定向到www.site.com/asd,这很奇怪。您确定以上是.htaccess文件中的唯一代码吗?在上面的任何地方,它都不会将其发送到
index.php/asd
。事实上,上面的第二条规则是将“index.php/asd”设置为
/ads
。如果您可以粘贴access.log的相关部分片段,这也将有助于调试它。这是我的实际代码:RewriteBase/RewriteCond%{REQUEST_FILENAME}上的RewriteEngine-f RewriteCond%{REQUEST_FILENAME}-重写规则^(.*)$/index.php/$1[L]选项+FollowSymlinks rewritecond%{http_host}^site.com[nc]RewriteRule^(.*)[r=301,nc]好的。因此,是您的this rule
RewriteRule^(.*)$/index.php/$1[L]
造成了麻烦。我在上面的回答中没有这一点。您可以对此进行评论并重试。根据您的评论,我已更新了我的答案,以包含所有更改。请将此内容复制/粘贴到您的.htaccess中,并让我知道。相关: