【问题标题】:Network Solutions hosting URL looks weird for Apache serverApache 服务器的网络解决方案托管 URL 看起来很奇怪
【发布时间】:2015-04-25 14:53:10
【问题描述】:

我试图美化我网站上动态生成页面的 URL,这样当用户访问虚拟 URL topicview/interesting-user-friendly-text 时,他会真正看到 topicview.php?topicid=123

我在 .htaccess 文件中添加了必要的代码,以将 URL 的 topicview/interesting-text 部分替换为 topicview.php?topicname=interesting-test,但正则表达式一直出错。

所以,我更改了正则表达式以返回整个 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

这个网址没有显示在topicnameGET参数中,只是一个普通的文件,比如index.php或者topicview.php,如果我只是访问网址index.php或者topicview.php的话

为什么 URL 在 Apache 服务器内部是这样表示的,我如何重写我的 mod_rewrite 代码来为 topicview.php?topicid=1 页面获取更用户友好的虚拟 URL?

谢谢

【问题讨论】:

  • 当您使用字符串参数访问 topicview 并放置 var_dump($_GET); 时显示什么
  • like "topicview.php?topicid=4" - 这就是字符串参数的意思吗?
  • 您能否使用此代码<?php phpinfo(); ?> 创建一个名为info.php 的文件,然后打开http://domain.com/info.php 以检查它的DOCUMENT_ROOT 值是多少。
  • 是的,data/1/2/323/232/823238/user/999999/htdocs 是 phpinfo() 报告的 Document_Root 值。有没有办法让 RewriteRule 的 Pattern 部分只查看 Request_URI,这是一个更简单的 topicview.php

标签: php apache .htaccess mod-rewrite hosting


【解决方案1】:

在 RewriteRule 中匹配的模式通常与 REQUEST_URI 相似,但您描述的行为表明它与 REQUEST_FILENAME 或类似文件匹配,即包含完整文档根的文件路径。

这表明您的 RewriteRule 不在 .htaccess 文件中,而是在 Apache 配置文件中的 or 规则中,对吗?

相反,您应该尝试使用 RewriteCond 获取您想要的值,这样您就可以保证您与 REQUEST_URI 匹配,例如:

RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . topicview.php?topicname=%1 [L]

注意 %1 而不是 $1,它允许您使用在 RewriteCond 中捕获的值。

【讨论】:

    【解决方案2】:

    对于友好的 URL,请在您的 .htaccess 文件中尝试此操作

    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/?topicview/([^/.*]+)/?$ topicview.php?topicname=$1 [L,QSA]
    

    【讨论】:

    • 感谢您的回答,但这并不能解决主要问题,即正则表达式 ^/?topicview/([^/.*]+)/?$ 不会匹配任何内容,因为 Apache 正在查看的 URL 是 data/1/2/323/232/823238/user/999999/htdocs/topicview.php 出于某种原因,而不仅仅是topicview/some-text
    • 您所描述的内容是一团糟。不确定您使用的是哪个版本的 Apache,但根据我的经验,RewriteRule 的第一部分总是查看 REQUEST_URI,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    相关资源
    最近更新 更多