3

我一直在谷歌搜索堆栈溢出试图解决这个问题,但无论我尝试什么都没有快乐并且不断收到错误。

我目前正在使用此代码重写地址以删除扩展名,我不久前从 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 作为背景文件,因此是第二个块)

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

我已经设法让我的测试站点www.website.com/page/使用另一个 SO 帖子中的代码加载,但只有当我输入该 url 时,如果我转到 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=0没有page/var=0图像、js 和 css 停止。当然,如果一切都可以用斜线,那就更好了。

谢谢

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

4

1 回答 1

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]
于 2013-07-24T17:24:05.280 回答