【问题标题】:mod_rewrite request to pretty urlmod_rewrite 请求漂亮的 url
【发布时间】:2011-08-11 23:31:49
【问题描述】:

假设我有一个格式为 myurl.com/index.php?m=1 的 url,我想将其映射到 myurl.com/aboutus ,这意味着,我希望浏览器在上面的栏中显示后一个 url。 所以我首先要做的是捕捉 aboutus 并将其映射到 index.php?m=1.

RewriteRule ^aboutus$ /index.php?m=1

现在,如果我输入 myurl.com/aboutus,则会显示正确的页面,但我也希望以相反的方式显示,所以即使我输入 /index.php?m=1,我也希望浏览器显示/关于我们。

如何使用 mod_rewrite 实现这一点?

感谢任何帮助!

干杯,
b

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:
    RewriteEngine on
    
    
    RewriteCond %{QUERY_STRING} =m=1
    RewriteCond %{REQUEST_URI} =/index.php
    RewriteRule ^.* /aboutus? [R=301]
    
    
    RewriteRule ^aboutus /index.php?m=1 [QSA]
    

    更新 #1

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} =m=1
    RewriteCond %{REQUEST_URI} =/index.php
    RewriteRule ^.* /kepek? [R=301]
    
    RewriteCond %{QUERY_STRING} =m=2
    RewriteCond %{REQUEST_URI} =/index.php
    RewriteRule ^.* /paroknak? [R=301]
    
    RewriteCond %{QUERY_STRING} =m=3
    RewriteCond %{REQUEST_URI} =/index.php
    RewriteRule ^.* /magam? [R=301]
    
    RewriteCond %{QUERY_STRING} =m=4
    RewriteCond %{REQUEST_URI} =/index.php
    RewriteRule ^.* /kapcsolat? [R=301]
    
    
    RewriteRule ^kepek /index.php?m=1&redirected=1 [QSA]
    RewriteRule ^paroknak /index.php?m=2&redirected=1 [QSA]
    RewriteRule ^magam /index.php?m=3&redirected=1 [QSA]
    RewriteRule ^kapcsolat /index.php?m=4&redirected=1 [QSA]
    

    【讨论】:

    • 这行得通,但我怎样才能为其他数字做呢?对不起,我的问题根本没有概括:(
    • 如果有很多这样的数字,我建议从 /index.php?m=... 请求重定向到 index.php 本身中正确命名的页面。只需检查 QUERY_STRING。
    • 只有4个,无法修改index.php :-/
    • 只有 4 个?然后为您拥有的每个号码创建如上所示的类似部分。
    • 如果我只是简单地复制上面的行(除了 1 行)并将 m=1 更改为 {2,3,4} 并将 aboutus 更改为其他字符串,我会得到 310:重定向太多。
    【解决方案2】:

    您是否希望将数字映射到 1->about、2->news...等页面?

    此外,您想要的行为听起来像是一个无限重定向循环。

    【讨论】:

    • 是的,这正是我想要的。对我来说,它看起来也像一个无限循环,但我不希望在 url 中显示任何参数,但我已经有了页面,并且在 html 中,链接是用参数编码的,就像常规的 get url,所以我不能修改他们很漂亮:(
    【解决方案3】:
    RewriteRule ^aboutus$ /index.php?m=1 [L]
    RewriteRule ^index.php\?m=1$ /aboutus [301,L]
    

    【讨论】:

    • 这条规则其实是倒退的。它给你一个 500 错误的原因是文件 'aboutus' 不存在。
    • 这是错误的,有几个原因。 500错误其实是因为第二条规则的flag不正确,应该是[R=301,L]。此外,由于用户指出规则在 .htaccess 文件中,因此 RewriteRules 不会与前导 / 匹配。此外,RewriteRule 的第一组无法与查询字符串匹配,因此需要使用 %{QUERY_STRING}RewriteCond
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    • 1970-01-01
    • 2012-05-28
    • 2010-11-01
    • 1970-01-01
    • 2021-03-15
    • 2013-07-28
    相关资源
    最近更新 更多