Multiple hyphen handling in .htaccess (URL rewriting)

陌路散爱 提交于 2019-12-24 17:46:00

问题


I have a problem with URL rewriting. I have written the following rule in my .htaccess file

RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2

It gives me URLs like that : http://localhost/actuco/c-628Y8x-france/ (featuring the ID of the category, and its slug). This URL works perfectly.

But, when I type this URL : http://localhost/actuco/c-xpS3cc-amerique-du-nord/ it doesn't works anymore. The cause of the problem must be the fact that the latter slug contains several hyphens (amerique-du-nord) whereas the first one (france) contains no hyphens at all (I tried with all my categories with slugs including multiple hyphens).

Does anyone know how to solve this please ?

Here is my complete .htaccess code

Options +FollowSymlinks

RewriteEngine On

RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
RewriteRule ^bio$ o.php [L]

Thanks in advance !


回答1:


You can try:

RewriteRule ^c\-([\w]+)\-([\w-]+)\/$ cat.php?id=$1&slug=$2


来源:https://stackoverflow.com/questions/53288730/multiple-hyphen-handling-in-htaccess-url-rewriting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!