我需要将下划线替换为破折号。我已经尝试过这个代码,并试图改变这里的一些参数,但我还没有与。htaccess语法友好,所以我没能做到自己:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule !.(html|php)$ - [S=6]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) /$1 [R=301,L]
我以前的。htacces代码是这样的,它工作得很好,但是直到现在才需要将下划线替换为破折号:
# Added for SME compatibility
Options +FollowSymLinks
# Ignore Indexation of apache
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules for ReRouting to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
我有这样的URL: http://mydomain.tld/nl_BE/get_quote
,语言(第一个参数)不应该用破折号代替,语言参数也可以改变,所以我不能在。htaccess文件中硬编码语言名称。所以我希望URL是这样的:
http://mydomain.tld/nl_BE/get-quote/some-random-title
http://mydomain.tld/nl_NL/about-us/some-random-text
提前感谢!
你可以使用这个通用递归规则:
Options +FollowSymLinks
RewriteEngine On
## RECURSION based rule
# if there is one underscore left then redirect
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_([^_]*)/?$ /$1/$2-$3 [NC,L,R=302]
# if there are more than one underscores then "repeatedly" replace it by -
RewriteRule ^([a-z]{2}_[a-z]+)/([^_]*)_(.*)$ $1/$2-$3 [L,NC]