用于连字动态网址的下划线

时间:2015-03-05 20:16:32

标签: php apache .htaccess mod-rewrite

我有以下现有的.htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
RewriteRule home index.php [L]
RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?RollID=$1&FrameID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RedirectMatch 301 /cache(.*) //$1
ErrorDocument 404 http://www.example.com/error.php

我有许多动态生成的网址正在被重写为

www.example.com/abc_def_ghi/或 www.example.com/abc_def_ghi/abc_def_ghi /

我想结束这个

www.example.com/abc-def-ghi/或 www.example.com/abc-def-ghi/abc-def-ghi /

我已经尝试将其添加到我的.htaccess

的末尾
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !.(html|php)($|?) [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]

但我得到500错误。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

重新排列您的规则:

ErrorDocument 404 http://www.example.com/error.php
RewriteEngine on

RewriteRule ^cache(.*) /$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

# if there is only one underscore then repalce it by - and redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=302]

# if there are more than one underscores then "repeatedly" repalce it by - and set env var
RewriteRule ^([^_]*)_([^_]*)_(.*) $1-$2-$3 [E=USCORE:1]

# if USCORE env var is set and there is no underscore left then redirect
RewriteCond %{ENV:REDIRECT_USCORE} =1
RewriteRule ^([^_]+)$ /$1 [L,R=302]

RewriteRule home index.php [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/?$ /index.php?TopicID=$1 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?RollID=$1&FrameID=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]