【问题标题】:Variables in .htaccess Redirect.htaccess 重定向中的变量
【发布时间】:2015-04-24 16:13:20
【问题描述】:

我已经找了一段时间了,但我不能完全解决这个问题......所以来吧......我已经编辑了我的网址,以便当我有一个网址时:

http://example.co.uk/Review.php?type=anime&type2=tv&id=109&title=shingekinobahamutgenesis

变成:

http://example.co.uk/anime/tv/109/shingekinobahamutgenesis/

使用 .htaccess。这一点进展顺利,新 URL 可以正常工作,但是旧 URL 不会重定向到此 URL,并且也可以正常工作。当人们使用我们的移动网站链接时,这会导致问题,因为它会将他们重定向到未更改的旧 URL。

我们的 .htaccess 代码是这样的:

RewriteEngine On    
RewriteCond %{HTTP_HOST} ^www\.example\.co.uk [NC]
RewriteRule (.*) http://example.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \ /(.*?)(%20)([^\?\ ]*)
RewriteRule ^ /%1%3 [L,R=301,NE]
RewriteRule    ^adminarea/?$ /adminarea.php [L,QSA]
RewriteRule    ^calendar/?$ /calendar.php [L,QSA]
RewriteRule    ^contact/?$ /contact.php [L,QSA]
RewriteRule    ^searching/?$    /searching.php    [NC,L,QSA]
RewriteRule    ^admin/([A-Za-z0-9-]+)/?$ /admin/$1.php [NC,L,QSA]
RewriteRule    ^([A-Za-z0-9-]+)/?$    /search.php?type=$1    [NC,L,QSA]
RewriteRule    ^view/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    /search.php?type=$1&im=$2&name=$3    [NC,L,QSA]
RewriteRule    ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    /search.php?type=$1&genre=$2    [NC,L,QSA]
RewriteRule    ^([A-Za-z0-9-]+)/list/([A-Za-z0-9-]+)/?$    /search.php?type=$1&letter=$2    [NC,L,QSA]
RewriteRule    ^extra/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    /rrww12o.php?type=$1&id=$2&genre1=$3&title=$4    [NC,L,QSA]
RewriteRule    ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$    /Review.php?type=$1&type2=$2&id=$3&title=$4    [NC,QSA,L]
RewriteRule    ^([A-Za-z0-9-]+)/?$ /$1.php [L,QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]

ErrorDocument 404 /error404.php
CheckSpelling on

RewriteBase /

# Check if mobile=1 is set and set cookie 'mobile' equal to 1
    RewriteCond %{QUERY_STRING} (^|&)mobile=1(&|$)
    RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]

    # Check if mobile=0 is set and set cookie 'mobile' equal to 0
    RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
    RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]

    # cookie can't be set and read in the same request so check
    RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
    RewriteRule ^ - [S=1]

    # Check if this looks like a mobile device
    RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|windows\ phone|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
    RewriteCond %{HTTP:Profile}       !^$

    # Check if we're not already on the mobile site
    RewriteCond %{HTTP_HOST}          !^m\.
    # Check to make sure we haven't set the cookie before
    RewriteCond %{HTTP:Cookie}        !\mobile=0(;|$)
    # Now redirect to the mobile site
    RewriteRule ^ http://m.example.co.uk%{REQUEST_URI} [R,L]

我尝试了多个版本的 [R=301] 和 [R] 重定向,但如果我只是将它添加到我有 [NC,QSA,L] 的末尾,它会将重写的链接重定向到原始链接(当然是错误的方式)。

需要 $1 的原因是这不是一个静态 URL,因为它涵盖了指向不同 MySQL 记录的多个链接,因为记录总是在增加,这必须像现在一样保持自动。

谁能帮忙?

【问题讨论】:

    标签: string .htaccess variables mod-rewrite redirect


    【解决方案1】:

    更新:不适用于 OP

    试试这个:

    RewriteEngine On
    
    RewriteCond %{HTTP_HOST} ^www\.example\.co.uk [NC]
    RewriteRule (.*) http://example.co.uk/$1 [R=301,L]
    
    RewriteCond %{THE_REQUEST} \ /(.*?)(%20)([^\?\ ]*)
    RewriteRule ^ /%1%3 [L,R=301,NE]
    
    # Rerwite to Review.php
    RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ /Review.php?type=$1&type2=$2&id=$3&title=$4 [NC,QSA,END]
    
    # Redirect old url to new url
    RewriteCond %{QUERY_STRING} ^type=([a-z0-9-]+)&type2=([a-z0-9-]+)&id=([a-z0-9-]+)&title=([a-z0-9-]+)$ [NC]
    RewriteRule ^Review\.php$ /%1/%2/%3/%4 [QSD,R,L]
    

    注意Review.php 规则上的END 标志 - 这可以防止无限循环。

    因此,如果您请求Review.php?type=anime&type2=tv&id=109&title=shingekinobahamutgenesis,您将被重定向到anime/tv/109/shingekinobahamutgenesis,然后将被重写。

    【讨论】:

    • 当我这样做时似乎会杀死网站...得到 500 Internal Server Error
    • 可能是其他字符串查询...我将编辑代码以显示所有字符串查询。
    • 您的代码无论如何都会引发内部服务器错误 - 没有我的新条件和规则...
    • 删除对我不起作用的。这是原始站点链接:anihelp.co.uk(原始的 htaccess 肯定有效)。
    • 我输入了QSD,这样原始查询字符串就不会附加到 URL 上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多