【问题标题】:Simple wild character for mod_rewrite?mod_rewrite 的简单通配符?
【发布时间】:2013-01-07 16:56:40
【问题描述】:

mod_rewrite 的新功能。我只是想做这样的事情:

# Turn on the rewriting engine
RewriteEngine On    

# use item php page to show the item
RewriteRule    ^parts/./[.]$    parts/item.php?id=$1    [NC]    

所以当一个人输入 example.com/parts/anything/anything 时,URL 将转到

example.com/parts/item.php?id=the2ndanything

据我了解,句号表示任何字符。另外,我尝试在句号之后添加一个 + (这应该意味着任何东西),但这仍然不起作用。

顺便说一句,我在一个临时 URL 上,因为我还没有更改我以前的网络主机,因此我的实际完整 URL 中有一个 ~。我希望这不是 mod_rewriting 的问题,因此是阻止它工作的问题。 .htaccess 文件位于网站 index.html 的根文件夹中。

【问题讨论】:

    标签: php html apache .htaccess mod-rewrite


    【解决方案1】:

    . 确实表示任何字符,但您必须在其后跟+(一个或多个)或*(零个或多个),并且您必须在@987654325 中捕获它@ 用作$1。使用[^/]+ 匹配直到但不包括/ 的所有字符以匹配但不捕获parts/ 之后的第一个目录。

    RewriteEngine On
    # (.*) captures the second "anything" in $1
    RewriteRule ^parts/[^/]+/(.*)$ parts/item.php?id=$1 [L]
    

    【讨论】:

    • 回答已接受。我知道这是正确的方法。你说的一切都是有道理的,但我似乎无法让它发挥作用。从服务器获取 404.... 嗯:/
    • @DaveFes add [R] 看看它试图重定向到什么
    • 重定向到以下内容:66.147.244.97/home8/davidssite/public_html/parts/item.php?id=1 提供 404 代码。我还应该提到,这个 url(目录或页面)的任何部分被删除,仍然会抛出 404,所以它似乎从一开始就关闭了!
    • 在重写parts/index.php之前添加一个/。奇怪,我不能从这里轻松诊断。
    • 刚刚更改了主机让我看看会发生什么,现在我的 IP 和站点已完全建立。没有更多的临时〜网址
    猜你喜欢
    • 2011-10-07
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多