: .htaccess 301 redirect with regex? How to do this with regular expression? Old -> New http://www.example.com/1.html -> http://www.example.com/dir/1.html http://www.example.com/2.html -> http://www.example.com/dir/2.html
How to do this with regular expression?
Old -> New www.example.com/1.html -> www.example.com/dir/1.html http://www.example.com/2.html -> www.example.com/dir/2.html http://www.example.com/3.asp -> www.example.com/dir/3.html http://www.example.com/4.asp -> www.example.com/dir/4.html http://www.example.com/4_a.html -> www.example.com/dir/sub/4-a.html http://www.example.com/4_b.html -> www.example.com/dir/sub/4-b.html
I've tried this:
Redirect 301 /1.html www.example.com/dir/1.html
Redirect 301 /2.html www.example.com/dir/2.html
Redirect 301 /3.asp www.example.com/dir/3.html
Redirect 301 /4.asp www.example.com/dir/4.html
Redirect 301 /4_a.html www.example.com/dir/sub/4-a.html
Redirect 301 /4_b.html www.example.com/dir/sub/4-b.html
More posts by @Speyer207
3 Comments
Sorted by latest first Latest Oldest Best
This is a rather more general solution, so in that respect it might not be what you are after, but it should redirect the URLs in your question.
RewriteEngine on
RewriteRule ^(d).(html|asp)$ /dir/.html [R=301,L]
RewriteRule ^(d)_([a-z]).html$ /dir/sub/-.html [R=301,L]
The first RewriteRule will match any single digit URL eg. "1.html" or "7.asp" and redirect to "/dir/1.html" or "/dir/7.html" respectively. The matches the first parenthesised sub pattern in the pattern that is matched.
The second RewriteRule matches a URL that contains a single digit followed by an underscore and then a single lowercase letter. The underscore is replaced with a hyphen in the redirect. eg. "6_g.html" is redirected to "/dir/sub/6-g.html".
this is what i came up with
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^((1|2).html|(3|4).asp)$ /dir/.html [R=301,L]
RewriteRule ^(4_([a-b])).html$ /dir/sub/.html [R=301,L]
</IfModule>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^1.html$ "http://example.com/dir/1.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^1_a.html$ "http://example.com/dir/sub/1-a.html" [R=301,L]
RewriteOptions inherit
That should work
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.