Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache Htaccess将连字符更改为下划线_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Apache Htaccess将连字符更改为下划线

Apache Htaccess将连字符更改为下划线,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我已经改变了我的链接结构,并试图301我的旧链接到新的链接,以阻止他们都得到404 旧链接 www.domain.comn/artistfirstname-artistsecondname/songfirstword-songsecondword 新链接 www.domain.com/mp3/artistfirstname_artistsecondname-songfirstword_songsecondworld.html 我试图重写旧链接以指向新链接,但我需要将第一个链接中的破折号更改为

我已经改变了我的链接结构,并试图301我的旧链接到新的链接,以阻止他们都得到404

旧链接

www.domain.comn/artistfirstname-artistsecondname/songfirstword-songsecondword
新链接

www.domain.com/mp3/artistfirstname_artistsecondname-songfirstword_songsecondworld.html
我试图重写旧链接以指向新链接,但我需要将第一个链接中的破折号更改为下划线,这样我就可以执行以下操作

RewriteRule ^(.*)/(.*)$ mp3/$1-$2.html
还需要考虑只有一个单词的艺术家和歌曲,这意味着他们在url中不会以破折号开头。

试试:

RewriteCond %{REQUEST_URI} !^/mp3/
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)$
RewriteRule ^(.*)-(.*)$ /$1_$2 [L]

RewriteCond %{REQUEST_URI} !^/mp3/
RewriteCond %{REQUEST_URI} !\-
RewriteRule ^([^/]+)/([^/]+)$ /mp3/$1-$2.html [L,R=301]
第一条规则将用下划线迭代替换破折号,但不会重定向。第二条规则确保所有破折号都消失,然后用破折号替换/进行重定向