【问题标题】:apache mod_rewrite - rewrite ruleapache mod_rewrite - 重写规则
【发布时间】:2013-09-08 12:49:09
【问题描述】:

我对 WOULD NOT WORK 区域中的 mod 重写规则有疑问 :) 我有一个项目站点,女巫只是一个 ajax 返回站点。 所以我喜欢在 sitename.com/my-test/project/test.html 上打开我的网站,并重写 sitename.com/my-test/project.html?index=test。 我知道在我重写之后,cms 的重写规则必须完成他的工作。我不确定这可能是问题所在,它们会交叉或崩溃。

如果我通过浏览器浏览 URL,一切都会正常工作,只有重写规则不起作用,我变得非常疯狂,找不到问题。

所以我希望有人可以帮助我。

在 .htaccess 中重写配置

    RewriteEngine On
    RewriteBase /

    #### WOULD NOT WORK ######
    # REWRITE AJAX PROJEKT
    RewriteCond %{REQUEST_URI}% ^my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
    RewriteRule ^my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1

    ### WORK CORRECTLY ####
    # REWRITE RULE FOR SEO FRIENDLY IMAGE MANAGER URLS
    RewriteRule ^files[0-9]*/imagetypes/([^/]*)/([^/]*) index.php?rex_img_type=$1&rex_img_file=$2

    # DON'T REWRITE IF REAL FILE, FOLDER OR SYMLINK EXISTS
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

    # EXCLUDE SPECIFIC FOLDERS FROM REWRITE ENGINE
    RewriteCond %{REQUEST_URI} !/files[0-9]*/
    RewriteCond %{REQUEST_URI} !/assets/.*
    RewriteCond %{REQUEST_URI} !/media/.*
    RewriteCond %{REQUEST_URI} !/redaxo/.*

    # REWRITE ALL OTHER REQUESTS TO INDEX.PHP
    RewriteRule ^(.*)$ index.php?%{QUERY_STRING} [L]

我也测试过

    RewriteCond %{REQUEST_URI}% ^/my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
    RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
    #OR JUST
    RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html$ my-test/projects.html?index=$1
    #OR
    RewriteRule ^/my-test/projects/(.*)$ my-test/projects.html?index=$1
    #AND CORRECT
    RewriteCond %{REQUEST_URI} ^/my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
    RewriteRule ^/my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
    #AND ---
    RewriteRule ^my-test/projects/([a-zA-Z0-9]+)\.html my-test/projects.html?index=$1 [L]
    

我没有找到答案,所以我真的希望有人能帮助我

谢谢大家

ng 莫克斯

【问题讨论】:

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


    【解决方案1】:
    RewriteCond %{REQUEST_URI}% ^my-test/projects/[a-zA-Z0-9]+\.html$ [NC]
    RewriteRule ^my-test/projects/[a-zA-Z0-9]+\.html my-test/projects.html?index=$1
    

    您的条件末尾有一个额外的%:%{REQUEST_URI}%。这意味着它必须是请求 URI 后跟 %,并且您在右侧的文本以 html 结尾,这意味着此条件将始终失败。

    此外,%{REQUEST_URI} 变量以/ 开头。请注意,当用于匹配重写规则中的模式时,前导斜杠已消失。

    其实你根本不需要这个条件。

    RewriteRule ^my-test/projects/([a-zA-Z0-9]+)\.html my-test/projects.html?index=$1 [L]
    

    【讨论】:

    • @user1807046 再次查看答案末尾的规则,您缺少捕获组(括号)
    • thx 也无法正常工作 :(((只有 RedirectMatch 正在工作,这不是 SEO 好 -> RedirectMatch ^/my-test/projects/([a-zA-Z0-9]+) \.html sitename.com/my-test/projects.html?index=$1
    • @user1807046 你是否打开了多视图?尝试在 htaccess 中的某处添加Options -Multiviews。
    • 失败的是CMS的SEO插件,它获取Header的REQUEST_URI并通过php转为正确的输出。因此,如果我只是重写 URL,标头中的 REQUEST_URI 是旧 URL,CMS 会将我带到错误的页面。如果我重写 php 代码,我可以修复它:-/ 或者我进行重定向而不是重写。感谢您的帮助
    猜你喜欢
    • 2023-03-18
    • 2012-07-22
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2011-02-23
    相关资源
    最近更新 更多