重写URL似乎写错了

时间:2016-09-18 20:03:16

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

我刚刚意识到我的一些重写网址停止了工作。我不知道为什么。

这是我的重写文件

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forums/([^/]*)/locate/([^/]*)/$ /forums.php?page=$1&tlocate=$2

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forums/$ /forums.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/$ /forums.php?page=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forums/action/([^/]*)/$ /forums.php?page=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forums/([^/]*)/section/([^/]*)/$ /forums.php?page=$1&sectionid=$2

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^register/([^/]*)/$ /register.php?page=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^account/([^/]*)/$ /account-page.php?page=$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^about-us/$ /about.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forums/([^/]*)/topic/([^/]*)/$ /forums.php?page=$1&topic=$2 [L]

ErrorDocument 400   /error.php
ErrorDocument 401   /error.php
ErrorDocument 403   /error.php
ErrorDocument 404   /error.php
ErrorDocument 500   /error.php

如果我去了www.localhost/account/

它只会将我发送到www.localhost/forums/

所以我下载了mozilla,看看我的htaccess是否停止了工作。 CSS没有在那里工作。然后我去了一个网站检查我的代码,并抱怨该行

RewriteRule ^about-us/$ /about.php

在那一行,我试图重写www.localhost/about.php  进入www.localhost/about-us/

没有任何成功。我做错了导致这个问题的是什么?

1 个答案:

答案 0 :(得分:0)

您可以在.htaccess

中使用
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^forums/([^/]*)/locate/([^/]*)/$ /forums.php?page=$1&tlocate=$2 [L]
RewriteRule ^changelogs/([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^forums/$ /forums.php [L]
RewriteRule ^forums/action/([^/]*)/$ /forums.php?page=$1 [L]
RewriteRule ^forums/([^/]*)/section/([^/]*)/$ /forums.php?page=$1&sectionid=$2 [L]
RewriteRule ^register/([^/]*)/$ /register.php?page=$1 [L]
RewriteRule ^account/([^/]*)/$ /account-page.php?page=$1 [L]
RewriteRule ^about-us/$ /about.php [L]
RewriteRule ^forums/([^/]*)/topic/([^/]*)/$ /forums.php?page=$1&topic=$2 [L]
RewriteRule ^([^/]*)/$ /forums.php?page=$1 [L]

ErrorDocument 400   /error.php
ErrorDocument 401   /error.php
ErrorDocument 403   /error.php
ErrorDocument 404   /error.php
ErrorDocument 500   /error.php

并在您的about.php HTML代码中添加此代码:

<base href="/">

因为您使用about-us/链接更改了根目录(对于css和其他亲属)。