duanpu1963 2015-03-05 20:16
浏览 44

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

I have the following existing .htaccess in place

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

I have many dynamically generated URLs which are being rewritten to this

www.example.com/abc_def_ghi/ OR www.example.com/abc_def_ghi/abc_def_ghi/

and i'd like to end up with this

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

I've tried adding this to the end of my .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]

but i'm getting a 500 error. Can anyone help?

  • 写回答

1条回答 默认 最新

  • duanke6249 2015-03-05 22:05
    关注

    Rearrange your rules like this:

    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]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口