论坛升级后重写URL

时间:2014-01-11 02:10:57

标签: regex apache .htaccess mod-rewrite redirect

我需要通过以下格式通过.htaccess重写网址:

http://www.example.com/showthread.php?threadid=50679
http://www.example.com/showthread.php?s=&postid=837557

http://www.example.com/threads/50679
http://www.example.com/posts/837557

问题是htaccess中已有两个类似的规则(来自之前的升级)需要保持原样

RewriteRule [^/]+/([\d]+)-.+-([\d]+).html showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/([\d]+)-.+.html showthread.php?t=$1 [NC,L]

它开始让多个规则有点混乱。有什么建议吗?

编辑:这是评论中每个请求的完整.htaccess。

#   Mod_security can interfere with uploading of content such as attachments. If you
#   cannot attach files, remove the "#" from the lines below.
#<IfModule mod_security.c>
#   SecFilterEngine Off
#   SecFilterScanPOST Off
#</IfModule>

RewriteEngine on

#Redirect contents of previous Xenforo directory to root
RewriteRule ^forums/threads(.*)$ /threads$1 [R=301,NC,L]
RewriteRule ^forums/forums(.*)$ /forums$1 [R=301,NC,L]
RewriteRule ^forums/posts(.*)$ /posts$1 [R=301,NC,L]
RewriteRule ^forums/members(.*)$ /members$1 [R=301,NC,L]

#One more fix from stackoverflow to fix dead /forums and /forums/ links
RewriteRule ^forums/?$ $1 [L,R=301]

#vBSEO to vBulletin URLs
RewriteRule [^/]+/([\d]+)-.+-([\d]+).html showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/([\d]+)-.+.html showthread.php?t=$1 [NC,L]

#suggested on stackoverflow to fix pre-2004 URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule     ^/showthread.php?threadid=(.*)$ /threads/$1 [L,R=301]
RewriteRule     ^/showthread.php?s=&postid=(.*)$    /posts/$1 [L,R=301]

# redirect site.com to www.site.com
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R]

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

<IfModule mod_rewrite.c>
    RewriteEngine On

    #   If you are having problems with the rewrite rules, remove the "#" from the
    #   line that begins "RewriteBase" below. You will also have to change the path
    #   of the rewrite to reflect the path to your XenForo installation.
    #RewriteBase /xenforo

    #   This line may be needed to enable WebDAV editing with PHP as a CGI.
    #RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
    RewriteRule ^.*$ index.php [NC,L]
</IfModule>

2 个答案:

答案 0 :(得分:3)

.htaccess中的重写规则不匹配前导斜杠。除此之外,你还有一些缺失的规则。

你完整的.htaccess应该是这样的:

ErrorDocument 401 default
ErrorDocument 403 default
ErrorDocument 404 default
ErrorDocument 500 default

RewriteEngine on
RewriteBase /

# redirect site.com to www.site.com
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [L,R=302]

#Redirect contents of previous Xenforo directory to root
RewriteRule ^forums/threads(.*)$ /threads$1 [R=302,NC,L]
RewriteRule ^forums/forums(.*)$ /forums$1 [R=302,NC,L]
RewriteRule ^forums/posts(.*)$ /posts$1 [R=302,NC,L]
RewriteRule ^forums/members(.*)$ /members$1 [R=302,NC,L]

#One more fix from stackoverflow to fix dead /forums and /forums/ links
RewriteRule ^forums/?$ $1 [L,R=302]

#vBSEO to vBulletin URLs
RewriteRule /([\d]+)-[^/-]+-([\d]+)\.html$ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule /([\d]+)-[^/.]+\.html$ showthread.php?t=$1 [NC,L]

RewriteCond %{THE_REQUEST} /showthread\.php\?(?:threadid|t)=([^\s&]+) [NC]
RewriteRule ^ /threads/%1? [R=302,L]

RewriteCond %{THE_REQUEST} /showthread\.php\?s=&postid=([^\s&]+) [NC]
RewriteRule ^ /posts/%1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^threads/([^/]+)/?$ /showthread.php?threadid=$1 [L,QSA]
RewriteRule ^posts/([^/]+)/?$ /showthread.php?s=&postid=$1 [L,QSA]

RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^ index.php [NC,L]

答案 1 :(得分:-1)

非常确定您可以将其放在控制.htaccess的{​​{1}}或Apache配置文件中,您将被设置。

www.example.com

同样,您也可以为其他示例执行此操作。

RewriteEngine on
RewriteRule ^/showthread.php?threadid=/(.*)$ http://www.example.com/threads/$1 [L,R=301]

编辑重读您的问题&amp;看到了你需要保留的规则,所以检查一下。规则的顺序是关键:

RewriteEngine on
RewriteRule ^/showthread.php?s=&postid=/(.*)$ http://www.example.com/posts/$1 [L,R=301]

或者这个怎​​么样?

RewriteEngine on
RewriteRule [^/]+/([\d]+)-.+-([\d]+).html showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/([\d]+)-.+.html showthread.php?t=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/showthread.php?threadid=/(.*)$ http://www.example.com/threads/$1 [L,R=301]
RewriteRule ^/showthread.php?s=&postid=/(.*)$ http://www.example.com/posts/$1 [L,R=301]