最初,编写此代码是为了创建"漂亮"URL。
当使用下面的代码时,mod_rewrite可以正常工作。
<Directory /var/www/vhosts/myurl.com/httpdocs>
Options Indexes FollowSymLinks
php_admin_flag engine on
php_admin_value open_basedir none
AllowOverride all
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTPS} !=off
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond $1 ^(register|account|logout|profile|edit_profile).*$
RewriteRule ^/(.*)$ https://myurl.com/?get=$1
它接受任何匹配的url,如https://myurl.com/register并将其重写为https://myurl.com/?get=register。将在浏览器中找到并显示相应的页面。
但是,我希望将原始url传递到浏览器。为了实现这一点,我在重写规则中添加了[PT]标志,如下所示:
RewriteRule ^/(.*)$ https://myurl.com/?get=$1 [PT]
这使得https://myurl.com/register(与上面的url相同)到浏览器,但不再显示页面。相反,它返回以下错误:
Bad Request
Your browser sent a request that this server could not understand.
Client sent malformed Host header
相关信息:
操作系统:Linux
服务器:Apache
控制:Plesk
目录:/var/www/vhosts/myurl.com/conf
文件:vhostrongsl.conf
我搜索了多个论坛和文章,但都没有结果。
文章1:使用mod_rewrite制作更漂亮的URL(包括[PT]标志的使用)
有人知道这里发生了什么以及如何解决吗?如何才能显示"漂亮"url?
经过两天的沮丧,我终于解开了谜题。
首先,我用的RewriteRule进行了实验,将其从更改为:
RewriteRule ^/(.*)$ https://myurl.com/?get=$1 [P]
至
RewriteRule ^/(.*)$ https://myurl.com/index.php?get=$1
没有任何标志。
每当触发重写时,就会产生一个无限循环。但我注意到循环是在没有重定向url的情况下发生的(保持"retty url")。进步
我决定简化代码。我从重写代码中删除了'LA-U:',如下所示:
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
至
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
成功了!感谢那些发表评论的人。希望这对将来的人有所帮助。