301 Redirects behaving strangely - .htaccess

Running into this issue where the redirect in the second line is redirecting to the URL in the first line.
Redirect 301 /academics/degrees http://mydomain.edu/folder1/location1/
Redirect 301 /academics/degrees/phd http://mydomain.edu/folder1/location2/
At first I thought it had something to do with the locations to be redirected containing hyphens, but haven't been able to find anything on that.
Does it have something to do with the locations to be redirected sharing the same folder/permalink structure?
I've never encountered this before and am totally lost. I tried RedirectMatch but that didn't have any effect.

It is because /academics/degrees matches both URLs and rule for /academics/degrees/phd never fires. Either change the order of your rule OR better use RedirectMatch with regex capability to match only desired URL pattern:
RedirectMatch 301 ^/academics/degrees/?$ http://mydomain.edu/folder1/location1/
RedirectMatch 301 ^/academics/degrees/phd/?$ http://mydomain.edu/folder1/location2/
Make sure to clear your browser cache before testing this change.

Related

301 redirection not redirecting to the right URL without a trailing slash

I have to manually redirect few URLs in my website1 to website2.
Below is my code in the .htaccess file of website1
Redirect 301 /post1/ https://www.website2.com/post1
When I enter https://www.website1.com/post1/ in the browser it's being redirected to https://www.website2.com/post1 successfully, as expected.
But, When I enter https://www.website1.com/post1 in the browser it's being redirected to https://www.website2.compost1, the slash is missing after https://www.website2.com
What could be done to solve this?
You can do this in a single rule using RedirectMatch that uses a regex to make trailing slash optional as this:
RedirectMatch 301 ^/(post1)/?$ https://www.website2.com/$1
Added benefit is avoiding repeat of post1 in source and target by using a capture group in source and using back-reference $1 in target.
Remove the trailing / from the Redirect.
Redirect 301 /post1 https://www.website2.com/post1
This redirect then works for both versions of the URL. See testing link here.
Using 2 Redirect URLs in this particular order solved it.
Redirect 301 /post1 https://www.website2.com/post1
Redirect 301 /post1/ https://www.website2.com/post1

Trailing slash must be present for .htaccess redirect 301 to take place - why?

I am trying to redirect the following:
Redirect 301 /fruit/ https://www.website.com/vegetables/
^ The above works great IF the user clicks a link. However, should the user type-in the URL and omit the trailing slash after 'fruit' then it throws a 404.
So, my question is, how do I make all reference to /fruit/ or /fruit work correctly under 301 protocol?
Thanks for all replies
Google was my friend: the answer is >
RedirectMatch 301 /fruit/?$ https://www.website.com/vegetables/

htaccess 301 redirect error

i have setup up a redirect
RedirectMatch 301 /data(.*) http://www.site.com/sites/default/files/datassheets$1
and i am getting the following error
http://www.site.com/sites/default/files/datasheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheetssheets/doc3542.pdf
when i rename the datasheets directory to something else it works but this is not an option
is this an apache error or am i doing something wrong
Your RedirectMatch regular expression /data(.*) is matching on every request and thus will continue indefinitely.
What the complete redirect rule will look like depends on your use-case. The following rule takes care of the endless loop issue and redirects the content following /data/ to the new structure at http://www.site.com/sites/default/files/datasheets/.
RedirectMatch 301 ^/data/(.+) http://www.site.com/sites/default/files/datassheets/$1
/data/my-cool-file =>
http://www.site.com/sites/default/files/datassheets/my-cool-file
the (.*) portion that you have after /data is matching sheets in your url. You are then taking that match and appending it on the redirect. That's what's giving you the repeating word. I'm guessing you're also redirecting to your own site, which is why it's repeating so many times.
What are you expecting to come after data that you want to append to the redirect? If it's a query string, you can add [QSA] as a flag at the end to maintain the query string.
Example:
RedirectMatch 301 /data/(.*) http://www.site.com/sites/default/files/datassheets/$1 [QSA]
Also, consider that you are telling everyone that any page that starts with data in any directory shouldn't exist, yet you are redirecting them to a page that matches the very same pattern you are supposedly getting rid of. You probably need to expand the regex to only match what you intend.

how to 301 redirect URL with %3E character

I have this weird URL, /andy-sixx-2012-short-hair%3Eandy. I've tried this:
Redirect 301 /andy-sixx-2012-short-hair%3Eandy http://mysite.com/page.html
but that fails. I can redirect something like /andy-sixx-2012-short-hairandy no problem so the issue seems to be the %3E character. Any help would be great. Thanks!
The URI is decodeded before modules like mod_alias or mod_rewrite gets to process them, thus you need to match against > and not %3E:
Redirect 301 /andy-sixx-2012-short-hair>andy http://mysite.com/page.html

Massive .htaccess Redirect

I got some problem on redirecting something.
I tried to use Redirect 301 /link/link/link to /link/link
Is there a way to make it more easier coz there are 100+ links I need to redirect.
Like this
/blog/category/energy-savings/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/uncategorized/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing
/blog/category/water-conservation/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/plumbing/about
To the Homepage?
Thanks
The directive:
Redirect 301 /link/link/link /link/link
Doesn't do what you think it does. It's mapping to nodes together, meaning a request for:
/link/link/link/foo/bar.html
gets redirected to:
/link/link/foo/bar.html
So maybe you need to fix that. You could try:
RedirectMatch 301 ^/link/link/link/?$ /link/link
so the nodes aren't connected like in the Redirect directive. As for trying to "fix" this problem of yours, you can try:
RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/\4/ /
This redirects anything that looks like: /blah1/blah2/blah3/same/same/same/ etc. to the homepage at /. The \4 matches the 4th path in the URI, so if anything after the 3rd path repeats, then it redirects.

Resources