www.mywebsite.com/folder为什么有效,但www.mywebsite.com/folder/不工作?

时间:2011-11-19 16:50:57

标签: .htaccess redirect

为什么 mywebsite.com/page 有效,但 mywebsite.com/page / 却没有?我们有一个htaccess条目可以删除文件的.php结尾,例如“mywebsite.com/new.php”可以简单地作为“mywebsite.com/new”访问。但是,如果添加了正斜杠(这似乎是某些搜索引擎由于某种原因添加了它或浏览器),则会显示我们的404错误页面。有任何想法吗?非常感谢!

以下是.htaccess文件中的所有重写文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-_]+)/?$ $1.php [NC,L]

rewriteCond %{HTTP_HOST} ^mywebsite.cz [NC]
rewriteRule ^(.*)$ http://www.mywebsite.cz/$1 [R=301,L]

rewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
rewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^mywebsite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mywebsite.com$
RewriteRule ^cs$ "http\:\/\/www\.mywebsite\.com\/index_cz\.php" [R=301,L]

RewriteCond %{HTTP_HOST} ([^.]+)\.mywebsite.com [NC]
RewriteCond %{HTTP:Accept-Language} (cs) [NC]
RewriteRule ^$ http://www.mywebsite.cz [L,R=301]

RewriteCond %{HTTP_HOST} ([^.]+)\.mywebsite.cz [NC]
RewriteCond %{HTTP:Accept-Language} (cs) [NC]
RewriteRule ^$ /index_cz [L,R=301]

RewriteCond %{HTTP_HOST} ^www.mywebsite\.com
RewriteRule ^([0-9]+)/?$ project.php?id=$1 [NC,L] # Handle project requests

RewriteCond %{HTTP_HOST} ^www.mywebsite\.cz
RewriteRule ^([0-9]+)/?$ project_cz.php?id=$1 [NC,L] # Handle project requests

3 个答案:

答案 0 :(得分:3)

你可以试试这个:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-_]+)/?$ $1.php [NC,L]

另外,您必须更改所有,以指向文件夹的根目录。例如:

<link rel="Shortcut Icon" type="image/ico" href="favicon.ico"/>
<script type="text/javascript" src="js/charactercounter.js"></script>

将成为:

<link rel="Shortcut Icon" type="image/ico" href="/favicon.ico"/>
<script type="text/javascript" src="/js/charactercounter.js"></script>

答案 1 :(得分:2)

当您编写mysite.com/page时,这会将您引导至根目录中的文档。但是,当您编写我的site.com/page/时,Apache会认为您引用了一个名为page的目录,然后在页面目录中查找文档。

答案 2 :(得分:0)

也许是这样......

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