【问题标题】:Unable to create a beautiful url with HTACCESS无法使用 HTACCESS 创建漂亮的 url
【发布时间】:2013-06-26 08:59:20
【问题描述】:

我想要做的是缩短这个网址: example.com?controller=iphone&action=xyz

进入:

example.com/iphone/xyz

这是我尝试过但不起作用的方法:

.htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/$ index.php?controller=$1&action=$2 [L]

【问题讨论】:

  • @Rikesh index.php 应该保留在 htaccess 中,它是路由脚本所在的位置。

标签: php mysql .htaccess


【解决方案1】:

没有明显的需要在你的模式中要求尾随斜杠,也没有限制它在以$ 结尾的行之后立即结束。另请注意,我将* 更改为+,因为您几乎肯定希望控制器和动作都至少有一个字符长。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?controller=$1&action=$2 [L]

【讨论】:

    【解决方案2】:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/(.*)$ index.php?controller=$1&action=$2 [L]
    

    【讨论】:

    • 请考虑详细说明您的代码与他的代码有何不同,以及为什么您的代码可以工作而他的代码不行。
    • @AmitGarg 感谢它的工作!但是如果我希望它也传递其他参数怎么办?例如:example.com/iphone/action?parameter1=1&parameter2=2 如果我试图访问该链接,它不会传递参数
    • @DanRevah 那么你需要QSA flag
    • 如果路径段超过 2 个,这可能不起作用。例如。 “/a/b/c”
    • RewriteRule ^(.*)/(.*) index.php?controller=$1&action=$2 [L,QSA]
    猜你喜欢
    • 1970-01-01
    • 2012-07-20
    • 2016-11-06
    • 2019-02-27
    • 1970-01-01
    • 2014-09-26
    • 2021-12-26
    • 2013-05-25
    • 2013-02-13
    相关资源
    最近更新 更多