I am using the following code in my .htaccess file to handle a re-direction, but it isn't working, a 404 appears. Can anyone tell me if the expression is incorrect, or if there is any other reason why this won't work? Other re-directions work on the same domain with the same .htaccess file.

Here is the format of the URL to be entered manually:


Here is the .htaccess code:

RewriteEngine on
RewriteRule ^([A-Za-z]+)/tags/?tag=([A-Za-z0-9-]+)$ /$1/tag/$2/ [R]

Thanks in advance.

1 answer


It was the question mark causing the problem but I didn't know how to correctly escape it. For anyone who needs it, here is the solution which catches the query string and stores the part after the equals for a back-reference via %1. The new question mark at the end of the URL to redirect to prevents the query string from being appended.

# Redirect this: blog/tags/?tag=a-tag
# to this:       blog/tag/a-tag/
# And this:      portfolio/tags/?tag=another-tag
# to this:       portfolio/tag/another-tag/
RewriteCond %{QUERY_STRING} ^tag=([A-Za-z0-9-]+)$ 
RewriteRule ^([A-Za-z]+)/tags/$ /$1/tag/%1/? [NC,R]
Answered over 7 years ago by Edward Williams