.htaccess重写和规则无法在结束时找出额外的斜杠

时间:2013-07-24 16:26:27

标签: apache .htaccess mod-rewrite url-rewriting

我一直在谷歌搜索堆栈溢出试图解决这个问题,但无论我尝试什么,都没有任何喜悦和不断的错误。

我目前正在使用此代码重写地址以删除扩展程序,我刚刚从简单网站上的SO帖子中获取它:

RewriteEngine on

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

# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]

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

# To internally forward /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.phtml -f
RewriteRule ^ %{REQUEST_URI}.phtml [L]

(我使用phtml文件作为ui文件,php使用php作为后台文件,因此第二个块)

这对我来说很好,但现在我在一个网站上,我想将变量提取到一个页面进行书签等,并且不希望地址有点像www.website.com/page?var=0

我已经设法让我的测试网站使用来自其他SO帖子的代码加载www.website.com/page/,但只有当我输入该网址时,如果我转到page.html并且不会加载附件,它将不会重写文件css,js(据我所知这是因为文件有相对路径)编辑:我现在明白这与-f ||有关!-F; 代码:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

我可以使用或不使用尾部斜杠,但我确实希望在存在变量时添加它,例如page?var=0page/var=0,而不停止图像,js和css。当然,如果它可以全部用斜线,那就更好了。

由于

编辑:当网站和.htaccess位于子文件夹中时也需要工作,例如:mysite.com/example/websitedemonstration/page

1 个答案:

答案 0 :(得分:2)

这个似乎对我很好,试一试:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

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

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

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