我试图为我的网站上动态生成的页面美化URL,这样当用户访问虚拟URL topicview/interesting-user-friendly-text
时,他实际上会看到topicview.php?topicid=123
我在.htaccess文件中添加了必要的代码,用topicview.php?topicname=interesting-test
替换URL的topicview/interesting-text
部分,但Regex一直不匹配。
所以,我更改了Regex以返回整个URL,这样我就可以理解为什么它不能使用以下代码:
#Allow for topicview/topic-name URLs
RewriteRule (.+)$ topicview.php?topicname=$1 [L]
然后我访问了topicview/user-friendly-text
。我不确定这是否是网络解决方案托管所独有的,但是,当我检查topicname
GET参数时,我得到了以下字符串作为回报:
data/1/2/323/232/823238/user/999999/htdocs/topicview.php
如果我只是访问URL index.php
或topicview.php
,该URL没有显示在topicname
GET参数中,只是一个常规文件,如index.php
或topicview.php
为什么URL在Apache服务器内部是这样表示的,以及我如何重写mod_rewrite
代码,为topicview.php?topicid=1
页面获得更用户友好的虚拟URL?
感谢
对于友好的URL,请在.htaccess文件中尝试
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?topicview/([^/.*]+)/?$ topicview.php?topicname=$1 [L,QSA]
RewriteRule中匹配的模式通常与REQUEST_URI相似,但您描述的行为表明它与REQUEST_FILENAME或类似的文件路径(包括完整文档根)匹配。
这表明您的RewriteRule不在.htaccess文件中,而是在Apache配置文件中的或规则中,对吗?
相反,您应该尝试使用RewriteCond获得您想要的值,这样您就可以保证与REQUEST_URI匹配,例如:
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . topicview.php?topicname=%1 [L]
请注意%1而不是$1,它允许您使用RewriteCond中捕获的值。