htaccess 301 redirect error - .htaccess

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.

Related

How can i 301 redirect a subfolder and everything after it?

I am trying to redirect a subfolder as well as anything after it to the home page.
For example:
example.com/subfolder/extra-stuff > example.com
The extra-stuff is constantly changing and auto generated, so I want the redirect to remove that as well.
I am using:
Redirect 301 /subfolder(.*) http://www.example.com
However, this will result in http://www.example.com/extra-stuff.
Is there a way I can say if /subfolder(and anything else after subfolder) redirect to home?
Thanks for any suggestions!
The Redirect directive uses simple prefix-matching and everything after the match is copied onto the end of the target URL (which is what you are seeing here). However, the Redirect directive also does not support regex syntax, so a "pattern" like (.*) on the end will actually match the literal characters (, ., * and ) - which shouldn't have worked in your example?!
You'll need to use RedirectMatch instead (also part of mod_alias), which does use regex, and is not prefix matching.
For example:
RedirectMatch 301 ^/subfolder http://www.example.com/
Any request that starts /subfolder will be redirected to http://www.example.com/ exactly.
You'll need to clear your browser catch before testing.
You tagged your question "Magento" (which is probably using mod_rewrite). You should note, however, if you are already using mod_rewrite for rewrites/redirects then you should probably be using mod_rewrite instead of mod_alias to do this redirect, since you can potentially get conflicts.
For example, the equivalent mod_rewrite directive would be:
RewriteRule ^subfolder http://www.example.com/ [R=301,L]
Note there is no slash prefix on the RewriteRule pattern. This would need to go near the top of your .htaccess file.

301 Redirects behaving strangely

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.

htaccess wildcard redirect to parent folder

a simple request I'm sure but can't for the life of me find an answer.
I would like the following...
http://example.co.uk/menu/item1, http://example.co.uk/menu/item2, http://example.co.uk/menu/item3 etc.
To redirect to http://example.co.uk/menu/.
Currently I am using the rule below but am getting a redirect loop on /menu/.
RedirectMatch 302 ^/menu/.*$ http://example.co.uk/menu/
How do I create a rule that redirects only what I require and leaves /menu/ accessible?
Thanks.
Tweak your regex a little by matching 1 or more characters after /menu/:
RewriteRule ^menu/(.+)$ /menu/ [L,R=301]

Over-zealous RedirectMatch in .htaccess file

I just converted a website that had thousands of bad links, and I have been cleaning up tens of thousands of indexing errors, some with redirect 301s and some with RedirectMatch. However, I am getting one error that puzzles me.
Either of these two lines of code in the .htaccess file
RedirectMatch 301 /faqs(.*) http://www.belleviewanimalclinic.com/pet-care-faqs/
RedirectMatch 301 /faq(.*) http://www.belleviewanimalclinic.com/pet-care-faqs/
cause this page
http://www.belleviewanimalclinic.com/denver-veterinarian-articles/faqs-about-pet-dentistry/
to be redirected here
http://www.belleviewanimalclinic.com/pet-care-faqs/
There are lots of old URLs that started with /faqXXXXXX that need to be redirected to the new FAQ page. However, I don't want this to apply to the article above.
Is there a way I can rewrite the RedirectMatch rules so that only URLs that start with /faq are redirected, rather than somewhere in the middle of the request URL?
Thanks!
Use a caret to match the start of a path:
RedirectMatch 301 ^/faqs?(.*) http://www.belleviewanimalclinic.com/pet-care-faqs/
I merged the two lines into one by making the "s" optional (by following it with a "?"). The caret should make it so the path has to begin with faq, not just include it.
Note: I initially removed the beginning slash since match strings generally start after the beginning slash, however I'm more used to RewriteRules than RedirectMatches and I believe the behavior may be different.

htaccess; /search/?q=test to /test

I have a similar situation to the one described in the title.
All that I need to do is map all requests in the form /search/?q=test to /test. This is because we are changing the way our search works to make it user friendly, but still want to allow for backward compatability (i.e. anyone that may have these links bookmarked etc).
However, thus far I have this:
RedirectMatch 301 /search/?q=(.*) /$1
And that doesn't work, but:
RedirectMatch 301 /search/(.*) /$1
does...
Any idea why?
Cheers.
have you tried with RedirectMatch 301 /search/\?q=(.*) /$1 ?
I am using the \ before the ? (to force it being used as a literal character) in case it gets treated as a special char (which it is, in regular expressions..)

Resources