【问题标题】:HTACCESS URL Rewrite not working making static urlHTACCESS URL重写无法制作静态url
【发布时间】:2017-03-22 14:43:54
【问题描述】:

我有问题我正在使用 htaccess 添加 index.php 并转换 url。

index.php 工作正常,但 url 没有转换。

给定网址:http://example.com/allmodels/Samsung-mobiles_80

我想转换成http://example.com/allmodels?st=Samsung-mobiles_80

我使用了以下语法。

1:

        Options +FollowSymLinks -MultiViews
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* index.php/$1
        RewriteRule ^allmodels/(.*)$ allmodels?st=$1 [NC,L]

2:

        Options +FollowSymLinks -MultiViews
        RewriteEngine on
        RewriteRule ^allmodels/(.*)$ allmodels?st=$1 [NC]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* index.php/$1 [PT,L]

3:

        Options +FollowSymLinks -MultiViews
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^allmodels/(.*)$ allmodels?st=$1 [NC]
        RewriteRule .* index.php/$1 [PT,L]

我尝试了很多语法,但都对我不起作用。

找不到错误 404 页面。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:

    我根据得到的新信息再次更新了建议的解决方案:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    RewriteRule ^/allmodels/(.*)$ /allmodels?st=$1 [P,NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$1 [L]
    

    RewriteRule .* index.php/$1 必须排在第二位,否则 /allmodels-call 将被重写为 index.php/allmodels/(.*)。或者你必须像我一样添加第三行来排除 /allmodels-calls。

    R=301 和 R=permanent 是一样的。使用你喜欢的任何东西。

    最后确保目标文件确实存在;)


    更新

    设置您的 rewrite.log 需要您在“RewriteEngine on”之后添加以下行

    RewriteLogLevel Debug
    

    完成此操作后,重新启动 apache 并调用您的 URL。然后查看 /var/log/apache2/rewrite.log,您将看到 rewrite-module 如何处理您的请求。要么发布结果,要么以这样的方式描述它,这将使其他人能够进一步支持你。谢谢:)

    UPDATE-2

    经过一番反复,很明显建议的规则本身不是问题。但是寻求最终解决方案的方式并不是最优的^^

    所以我现在更改了我的解决方案以适应批准的正确和工作版本,使用“P”表示代理而不是“R”用于重写以隐藏用户写入的原始 URL。

    【讨论】:

    • 哪个规则先写 RewriteRule ^/allmodels/(.*)$ /allmodels?st=$1 [R=permanent,NC,L] OR RewriteRule .* index.php/$1 [PT,L ]
    • 谢谢。但是您的代码不起作用并且目标文件存在。使用您的代码后,我找到了example.com/home/xyz/public_html/index.php url。
    • 您究竟调用了什么来访问 example.com/home/xyz/public_html/index.php?我想不出任何通过我建议的规则并最终出现的案例^^ 你的 DocumentRoot 是什么?是否还有其他 ProxyPass、处理程序或重写来干扰建议的规则?你能确保这些规则甚至经过测试吗? --> 将 RewriteLogLevel 5 添加到您的配置中 --> 重新加载/重新启动 apache
    • 查看选项 +FollowSymLinks -MultiViews RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$1 此代码有效,但我想添加更多重写不起作用的规则
    • 因此,您用于检查文件或目录是否存在的 RewriteCond 仅适用于 RewriteRule .* index.php/$1 ?再说一遍: .* 捕获所有可能的 URI,包括 /allmodels ,因此这也被重写了。所以你的第二个选项(你原来的帖子)应该是它,如果你添加 [R=301,NC,L] 和一个“/”来启动 URIs --> RewriteRule ^/allmodels/(.*)$ /allmodels ?st=$1 [R=301,NC,L]
    【解决方案2】:

    最后我得到了我的解决方案,我使用了下面对我有用的代码。

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    RewriteRule ^allmodels/(.*)$ /allmodels?st=$1 [P,NC,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php/$1 [L]
    

    谢谢鱼儿。

    【讨论】:

      猜你喜欢
      • 2012-10-02
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 2011-06-02
      • 1970-01-01
      相关资源
      最近更新 更多