我最近在.htaccess中应用了一些重写规则,以使链接看起来更漂亮。在本地主机上工作时一切正常。
但是,由于我的网站即将完工,所以我想使用真实域名进行模拟。我在Windows上使用xampp。我更改了Apache conf文件并添加了虚拟主机。该虚拟主机运行正常。像现在这样,而不是本地主机,我的网站从我的自定义域加载。我在Windows中更改了主机文件以使其正常工作。
但是在更改为自定义域后,我的网站运行正常,但是重写规则完全是一团糟。我面临一个奇怪的问题,例如,我有以下规则:
RewriteRule ^login(/)?$ http://example.com/account/login [L]
当我在浏览器的地址栏中输入以下内容时:
example.com/login
上面的链接应该在“ localhost”上正常工作。但是它不是重定向到“ example.com/login
”,而是重定向回“ example.com/account/login
”,这显然不应该发生。
我的所有重定向规则(包括那些由GET组成的规则)都在发生这种情况。它们还被重定向回包含$ id =(某些值)的裸链接。
我的完整htaccess文件如下:
# Redirecting If Client/Server Failure Occurs
RewriteEngine on
ErrorDocument 400 http://example.com/failure/bad-request
ErrorDocument 403 http://example.com/failure/access-denied
ErrorDocument 404 http://example.com/failure/not-found
ErrorDocument 500 http://example.com/failure/internal-server-error
ErrorDocument 502 http://example.com/failure/bad-gateway
ErrorDocument 503 http://example.com/failure/service-unavailable
ErrorDocument 504 http://example.com/failure/gateway-timeout
# Deny access to .htaccess
<Files .htaccess>
Order allow,deny
Deny from all
</Files>
# Disable directory browsing
Options -Indexes
# Remove Page Extension
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +MultiViews
RewriteEngine On
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
</IfModule>
# End of remove page extensions
# Rewriting SEO Friendly URL
RewriteEngine On
RewriteRule ^article/([0-9]+)/([a-zA-Z0-9\-]+)(/)?$ http://example.com/articlepage?id=$1&title=$2 [L]
RewriteRule ^profile/([a-zA-Z0-9]+)(/)?$ http://example.com/profilepage?username=$1 [L]
RewriteRule ^admin/preview/([0-9]+)/([a-zA-Z0-9]+)(/)?$ http://example.com/admin/final-preview?articleID=$1&flag=$2 [L]
RewriteRule ^register(/)?$ http://example.com/account/register [L]
RewriteRule ^login(/)?$ http://example.com/account/login [L]