【问题标题】:.htaccess multiple variable rewriting.htaccess 多变量重写
【发布时间】:2013-11-28 12:07:59
【问题描述】:

我设法像这样将 .htaccess 放在一起,它工作正常

RewriteRule ^$ /splash [L,R=301]        
RewriteCond %{HTTP_HOST}  ^example.com
RewriteRule (.*) http://www.examle.com/$1 [R=301,QSA,L]
RewriteRule ^([A-Za-z0-9-]+)?$ /index.php?cat=$1 [L]

这会将 example.com/one 之类的内容重定向到 http://www.example.com/index.php?cat=one

但是,我想为我当前的项目添加更多功能,以便解析多个变量。例如:

example.com/category-one?another_variable=some_data&even_more=more_data, etc... 

解析为

http://www.example.com/index.php?cat=category-one&another_variable=some_data&even_more=more_data

另外,这样的解决方案对 seo 友好吗?

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    在最后一条规则中添加QSA标志:

    RewriteRule ^$ /splash [L,R=301]
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
    RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
    
    RewriteRule ^([a-z0-9-]+)/?$ /index.php?cat=$1 [L,NC,QSA]
    
    • QSA(查询字符串附加)标志在添加新参数时保留现有查询参数。

    【讨论】:

    • 似乎它适用于前两个变量(cat 和第二个),但如果我再添加一个,那么第二个变量的内容会像 some_data?even_more=more_data 一样填充
    • QSA 标志将与 任何数量 的查询参数一起使用。提供一个无效的 URL 示例。
    • 此 URL 不正确:autobazar-ns.cz/garance-kvality?car=abcd?car2=aaa 因为 URI 中有 2 个?
    【解决方案2】:

    试试这个 RewriteRule ^mydir/([0-9]+)/(.*)$ myfile.php?var1=$1&var2=$2 [NC,L]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多