.htaccess强调为连字符,但第一个参数不应该被替换,它是动态参数

时间:2015-09-17 14:10:25

标签: php apache .htaccess mod-rewrite

我需要将下划线替换为破折号。我已经尝试过这段代码,并尝试在这里更改一些参数,但我还没有使用.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

我有这样的网址: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

提前致谢!

1 个答案:

答案 0 :(得分:1)

您可以使用此通用递归规则来代替那些多个规则:

table2