Redirecting A File Anywhere In The Hierarchy - .htaccess

So, apparently some mobile ads drop a request for mraid.js into their page loads. That feels ridiculous to me, but there's not much I can do about that exactly. It looks like this:
If the page is /something, the request comes in as /something/mraid.js
But that also continues for cases like /something/else/mraid.js
No matter what the URL, it tacks on mraid.js.
I added a couple lines like this to the .htaccess file:
RedirectMatch 301 (.*)\mraid.js$ /mraid.js
or
RedirectMatch 301 ^\mraid.js$ /mraid.js
or
RewriteCond %{DOCUMENT_ROOT}/(.*)/mraid.js -f
RewriteRule ^(.*)$ /mraid.js [R=301,L]
With the hopes of redirecting them to a global (and blank) mraid.js file. This is just to load a file to stop the 404 errors in analytics, from what I'm told when mraid.js is needed it's loaded locally from within the mobile ad/app.
But none of those .htaccess rules seem to catch and redirect as I expect. Any ideas on how to always match mraid.js in the URL and pass it back to root?

After a bit more head-scratching, I figured it out. It was a combination of all three things that solved it for me. First, it had to match the pattern with ^(.*), then [L,R=301]. L for "this is the last rule", and R for "it's a permanent 301 redirect."
RewriteRule ^(.*)/mraid\.js$ /mraid\.js [L,R=301]

Related

HTACCESS 301 redirect keep sending to the wrong page

I am trying to redirect an old page from a website I have redesigned, to the new one, but it's not working.
Here's my 2 lines of code in the .htaccess file regarding that domain:
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
RewriteRule ^/deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L,QSA]
If go on the solutionsgtr.ca/deaneco/contact URL, it gives me the following page:
http://solutionsgtr.ca/fr/deaneco/accueil.html/contact
The first rule works though (deaneco/ to solutionsgtr.ca/fr/deaneco/accueil.html).
I feel like both lines are being mixed together and are giving me the wrong page, that doesn't exist so I get a 404 error.
There are a couple of issues here:
The Redirect directive (part of mod_alias) is prefix-matching and everything after the match is appended on the end of the target URL. This explains the redirect you are seeing.
The RewriteRule (mod_rewrite) pattern ^/deaneco/contact will never match in a .htaccess context since the URL-path that is matched does not start with a slash. So, this rule is not doing anything currently.
You should avoid mixing redirects from both modules since they execute independently and at different times during the request (mod_rewrite executes first, despite the apparent order of the directives).
Either use mod_alias, ordering the directives most specific first:
Redirect 301 /deaneco/contact http://solutionsgtr.ca/fr/deaneco/contact.html
Redirect 301 /deaneco http://solutionsgtr.ca/fr/deaneco/accueil.html
NB: You will need to clear your browser cache, since the erroneous 301 (permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
OR, if you are already using mod_rewrite for other redirects/rewrites then consider using mod_rewrite instead (to avoid potential conflicts as mentioned above):
RewriteEngine On
RewriteRule ^deaneco/contact$ http://solutionsgtr.ca/fr/deaneco/contact.html [R=301,L]
RewriteRule ^deaneco$ http://solutionsgtr.ca/fr/deaneco/accueil.html [R=301,L]
The QSA flag is not required, since the query string is passed through to the substitution by default.
The order of the RewriteRule directives are not important in this instance, since they match just that specific URL.
If go on the solutionsgtr.ca/deaneco/contact URL
If you are redirecting to the same host then you don't need to explicitly include the scheme + hostname in the target URL, since this will default.

URL rewrites issues

We are having a problem with URL rewrites on an apache server using .htaccess.
Goal: to have the following URL stripped of its category & subcategory while leaving the generic redirect in place.
Test 1:
Redirect 301 /category/subcategory/product http://www.site.com/product
Redirect works perfectly. A single redirect to the desired page.
Test 2:
RedirectMatch 301 ^/category/subcategory/.*$ http://www.site.com/category/subcategory
Redirect on its own works perfectly for all URLs desired.
The problem is when we have both URLs in a clean .htaccess file, and the redirects are in the proper order (specific first, then general), the general redirect is being used.
Test 3:
Redirect 301 /category/subcategory/product http://www.site.com/product
RedirectMatch 301 ^/category/subcategory/.*$ http://www.site.com/category/subcategory
When we visit www.site.com/category/subcategory/product, the result is www.site.com/category/subcategory/product, That is not the desired result. Instead, we want the URL to be www.site.com/category/subcategory/product,
We have even tried modified the Redirect to:
Redirect 301 /category/subcategory/product http://www.site.com/product [L]
It made no difference.
Please help!
EDIT: Added 3/25/2014
What we are trying to do is provide specific redirects for a group of known products from their old product page to the new product page. We are also trying to add a "catch all" redirect for the remaining unknown products to the category page.
Here is an actual example redirect which works:
Redirect 301 /womens/western-dresses/stetson-cream-empire-waist-ls-western-dress http://www.site.com/stetson-cream-empire-waist-ls-western-dress
If the above redirect is added to the .htaccess file, it works perfectly on its own.
Here is a second example redirect which works:
RedirectMatch 301 ^/womens/western-dresses/.*$ http://www.site.com/womens/western-dresses
The problem is if we have both of the rules together in .htaccess, in the same order as above, the second rule is always triggered. We try to access www.site.com/womens/western-dresses/stetson-cream-empire-waist-ls-western-dress and the result is www.site.com/womens/western-dresses instead of the desired result of www.site.com/stetson-cream-empire-waist-ls-western-dress
For clarity:
if we remove the .htaccess file, the URL 404s
if only the first rule is listed, it triggers perfectly
if only the second rule is listed, the second rule triggers perfectly
if both rules are listed, the second rule triggers.
We have deleted all redirects from the .htaccess file. The only redirects are the below two lines. The issue remains where the first redirect is ignored. We have tried changing the start of the first redirect to ^/womens and ^womens but that change had no effect.
Redirect 301 /womens/western-dresses/stetson-cream-empire-waist-ls-western-dress http://www.site.com/stetson-cream-empire-waist-ls-western-dress
RedirectMatch 301 ^/womens/western-dresses/.*$ http://www.site.com/womens/western-dresses
Your post is a little confusing, so I may be misunderstanding what you are trying to do.
If memory serves, you should not include a leading slash in your pattern when using these directives in a .htaccess file. That usage is reserved for httpd.conf. When these directives are used in a .htaccess file, the leading path components have already been stripped by mod_access. I am guessing this is the cause of your troubles.
For example, this should work (not tested):
Redirect 301 ^category/subcategory/product http://www.site.com/product
RedirectMatch 301 ^category/subcategory/.* http://www.site.com/category/subcategory
As an aside, [L] is mod_rewrite lingo. "Redirect" and "RedirectMatch" are part of mod_access.
EDIT 3/25:
Redirect and RedirectMatch can be fussy when used in .htaccess files, particularly when dealing with non-existent folders and mixed directives. Can I suggest you move directly to mod_rewrite? While it has a steep learning curve, you will never go back once you get the hang of it.
# Assuming you are in a .htaccess under DocumentRoot:
RewriteEngine On
RewriteRule ^category/subcategory/product1\.html$ /product1.html [R=301,L]
RewriteRule ^category/subcategory/product2\.html$ /product2.html [R=301,L]
RewriteRule ^category/subcategory/.* /category/subcategory [R=301,L]
As an aside, this looks like a good candidate for RewriteMap, although you will need to declare the map in your httpd.conf.

Remove word from end of URL using htaccess

I have a bunch of URLs coming up as .../undefined which get a 404 error, and I'd like to know how to removed the word "undefined" from the end of the URL using .htaccess
I have looked at many posts about removing extensions, queries, and URL-internal folders, and have tried adapting those rules to suit my purposes, but so far I haven't been able to make it work. Any ideas?
Sample URL: http://www.theveggietable.com/blog/vegetarian-recipes/sandwiches/true-veggie-burgers/undefined
I just want the word "undefined" to be stripped out so that the user is automatically redirected to http://www.theveggietable.com/blog/vegetarian-recipes/sandwiches/true-veggie-burgers/
Thanks!
Try:
RewriteEngine On
RewriteRule ^(.*)/undefined/?$ /$1/ [L,R=301]
or:
RedirectMatch 301 ^/(.*)/undefined/?$ /$1/
redirect 301 should do the trick
redirect 301 /undefined http://www.theveggietable.com/blog/vegetarian-recipes/sandwiches/true-veggie-burgers/
You can use the mod_rewrite module in your .htaccess file to modify the URL. Although, it seems like you're doing something wrong in the back-end if you're getting those kinds of URL's. I would suggest looking into why you get undefined in the first place.
For more information on mod_rewrite, see this blogpost.

trying to redirect a link in joomla or .htaccess

we moved our joomla site and rebuilt. in the process a link got moved that we need to be as it was before.
before:
www.mysite.org/kindergym
now it lives here:
www.mysite.org/education/kindergym
it would seem that it would be easy to go into com_redirect and do this. however, it only works for the following
mysite.org/kindergym without the www
with the www attached writing the old url returns a 404 error page, not a redirect.
i tried to make a separate redirect with the www too and it wouldnt let me. i tried a separate module with no success and have played around with the .htaccess file (although i am not very knowledgeable about htaccess).
could someone explain the reason why this would be an issue? the difference between the two. i tried calling my host and they were less than helpful and actually told me what i wanted to do couldnt be done LOL.
thanks.
I take it the solution you have would work if you redirect the entire mysite.org to www.mysite.org?
If so, create a .htaccess file in the website root. Put the following inside it:
########## Begin - Redirecting non-www request to www
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.org [NC]
RewriteRule (.*) http://www.mysite.org/$1 [L,R=301]
#
########## End - Redirecting non-www request to www
You also need to make sure mod_rewrite is enabled on the apache-server, but I think most providers support that.
I suggest you post your full .htaccess here. However I think all you need is this rule:
RewriteRule ^(?!education/).*)$ education/$1 [L,NC]
The other two answers are good! but better implement 301 redirect in httpd.conf since it's compiled once on server restart. The same code in .htccess is interpreted for each and every HTTP request!

Some redirects work, some don't...all same syntax?

OK, so I have an .htaccess file with over 4000 redirects for a Magento ecommerce store, some of the 301s work and some don't.
Here's a sample:
redirect 301 /1-watt-12-volt-led-bullet-spotlight.aspx http://www.my-new-webstore.com
redirect 301 /1-watt-12-volt-led-waterfall-light.aspx http://www.my-new-webstore.com
The second one is returning a 500 error, why? It's almost exactly the same as the one above it (just a different product); when I put them both live it returns a 500 error but when I comment it out, no error.
Could there be some other file not enabling the redirect?
Thanks!
Maybe it would be better to use:
RewriteRule (.*) http://www.my-new-webstore.com [R=301,L]
For all of them, or something like
RewriteRule ^1-watt-12(.*) http://www.my-new-webstore.com/1-watt-12$1 [R=301,L]
If you need things more precise (haven't checked last one, and is written from memory, so read the docs if it doesn't work!)

Resources