301 Redirect not working in some cases - .htaccess

I've made a few changes to my URL structure. Here is my htaccess file
Redirect 301 /top100 http://www.example.com
Redirect 301 /monthlytop10 http://www.example.com/monthlytop
Redirect 301 /top100/Hardwell http://www.example.com/top100/hardwell
Redirect 301 /top100/Armin%20van%20Buuren http://www.example.com/top100/armin-van-buuren
Redirect 301 /top100/David%20Guetta http://www.example.com/top100/david-guetta
This works for the first 2 cases but doesn't work for the rest. It redirects www.example.com/top100/Armin%20van%20Buuren to www.example.com/Armin%20van%20Buuren
If I remove the first line in the htaccess file, it doesn't redirect at all and gives a 404 error. What's wrong here?

That is because Redirect directive doesn't support regex. Use RedirectMatch instead to match exactly what you want:
RedirectMatch 302 "^/top100/?$" /
RedirectMatch 302 "^/monthlytop10/?$" /monthlytop
RedirectMatch 302 "^/top100/Hardwell/?$" /top100/hardwell
RedirectMatch 302 "^/top100/Armin van Buuren/?$" /top100/armin-van-buuren
RedirectMatch 302 "^/top100/David Guetta/?$" /top100/david-guetta

Related

htaccess Redirect 301 redirects to wrong url

I've created a bunch of 301 redirects in my .htaccess, for example
Redirect 301 / /de/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
and so on.
When I enter
www.old.com/site_1/ (wrong)
I get directed to
https://www.new.com/de/site_1/ (services folder missing)
It seems that the parent folder is missing in the redirected URL. Same for all other sites that reside in folders.
Redirect 301 / /de/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
Since the mod_alias Redirect directive is prefix-matching, a request for www.old.com/site_1/ would actually get caught by your first (most general) rule. And everything after the match (ie. site_1/) gets appended onto the end of the target URL (ie. /de/), so the resulting redirect becomes /de/site_1/ (but not to new.com as you've stated?).
You could resolve this by reversing the directives, to have the most specific matches first. For example:
Redirect 301 /services/site_2/ https://www.new.com/de/services/site_1/
Redirect 301 /site_1/ https://www.new.com/de/company/site_1/
Redirect 301 / /de/
Or, as you mentioned in comments, use RedirectMatch instead - which is not prefix-matching and matches against a specific regex instead. Although you will still need to modify the pattern. Something like:
RedirectMatch 301 ^/$ /de/
RedirectMatch 301 ^/site_1/$ https://www.new.com/de/company/site_1/
RedirectMatch 301 ^/services/site_2/$ https://www.new.com/de/services/site_1/
Although this now matches the exact URL, which may or may not be what you require.

Very Simple 301 Redirects First line only working in htaccess

Have searched high and low for an answer - 301 redirects seem a common issue on here but after much reading I'm stumped!
I have a simple htaccess file with three 301 redirects in:
redirect 301 example1.htm http://www.example.co.uk/newexample1.htm
redirect 301 example2.htm http://www.example.co.uk/newexample2.htm
redirect 301 example3.htm http://www.example.co.uk/newexample3.htm
However, only the first 301 redirect is working. Subsequent redirects aren't being followed.
The site doesn't have a CMS - is all just pure css/html.
Any suggestions?
Try these rules with RedirectMatch to target specific page with regex:
RedirectMatch 301 ^/example1\.htm$ http://www.example.co.uk/newexample1.htm
RedirectMatch 301 ^/example2\.htm$ http://www.example.co.uk/newexample2.htm
RedirectMatch 301 ^/example3\.htm$ http://www.example.co.uk/newexample3.htm

Order of 301 Redirects?

I am curious if someone could educate me to better understand why the following does not work based on the order.
When I have the redirect for /contact first the location pages fail to redirect properly.
Redirect 301 /contact http://www.example.com/contact-us
Redirect 301 /index.php/contact/location1 http://www.example.com/contact-us/location1
Redirect 301 /index.php/contact/location2 http://www.example.com/contact-us/location1
When I have it after the locations, they work normally. Why is this?
Redirect 301 /index.php/contact/location1 http://www.example.com/contact-us/location1
Redirect 301 /index.php/contact/location2 http://www.example.com/contact-us/location1
Redirect 301 /contact http://www.example.com/contact-us
It is because other 2 URLs also have /contact in them.
It is always better to use RedirectMatchdirective that with capability to use regex so that you can match exactly what you need.
Using RedirectMatchdirective following will also work:
RedirectMatch 301 ^/contact/?$ http://www.example.com/contact-us
RedirectMatch 301 ^/index\.php/contact/location1/?$ http://www.example.com/contact-us/location1
RedirectMatch 301 ^/index\.php/contact/location2/?$ http://www.example.com/contact-us/location1

htaccess: Redirect vs. Redirect 301

I was just looking in one of my .htaccess files and noticed the following:
Redirect /old-page /new-page
I was expecting to see Redirect 301 or Redirect 302, but it only said Redirect and works just fine.
I'm curios if 301 or 302 isn't indicated what does the redirect default to? 301? 302? Something else?
This line:
Redirect /old-page /new-page
Means use 302 since 302 is default value here.
To use 301:
Redirect 301 /old-page /new-page
See 301 directive documentation

redirect same sub by slash in htaccess

How to redirect as:
Redirect 301 /ruler/ http://www.sample.com/ruler/
Redirect 301 /ruler/red/review http://www.sample.com/product_review/flexible-ruler/
Redirect 301 /ruler/read/ http://www.sample.com/read/soft-ruler/
with above code, it not work correct. with
Redirect 301 /ruler/red/review will to http://www.sample.com/ruler/
Thanks about helps!
You need to put Redirect 301 /ruler/red/review line before Redirect 301 /ruler/, read more about Order of Processing

Resources