【问题标题】:Regexp with mod_rewrite带有 mod_rewrite 的正则表达式
【发布时间】:2014-11-22 18:21:13
【问题描述】:

我花了大约一小时,阅读了很多教程(MDN,博客。),

还是说不出来,这就是我想要的:

http://localhost/post/create
http://localhost/post/create/
http://localhost/post/create/?ID=123

真正的路径是:

http://localhost/includes/function/post/create.php

所以我尝试了:

RewriteRule ^post/(.*)(/.*)?$ includes/function/post/$1.php$2

看起来不错,但如果 URL 以 '/' 或 '/?ID=123' 结尾,它将不起作用

有时它会变得更糟,只是给我一个 HTTP 500 错误,

请问有什么解决办法吗?

编辑:我希望只用一条规则就可以使用以上三种方式,可以吗?

edit2:我意识到我只需要在我的 .htaccess 中删除 -MultiView

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您可以在根 .htaccess 中使用此规则:

    RewriteEngine On
    
    RewriteRule ^post/([^/]+)/?$ /includes/function/post/$1.php [L,QSA]
    

    【讨论】:

    • 对不起,还是不行,希望我能用上面三种方式只用一条规则。
    • 它现在工作正常,我意识到我只需要删除-MultiViews
    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多