I need make this redirect to my webserver:

www.hostname.com/path1/path2/123456789 to www.hostname.com/search?q=123456789

I have inserted this rule to htaccess file:

RewriteRule ^/path1/path2/([0-9]+)$ /search?q=$1 [L,R=301]

but is doesn't work (the number is a variable).


In a .htaccess file, you should not have a leading slash: "the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution".

I don't know in which directory your .htaccess lives, but you should remove the prefix for that directory:

RewriteRule ^path1/path2/([0-9]+)$ /search?q=$1 [L,R=301]

… or …

RewriteRule ^path2/([0-9]+)$ /search?q=$1 [L,R=301]

… or …

RewriteRule ^([0-9]+)$ /search?q=$1 [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.