Why does this redirect apply? - .htaccess

I have a simple Redirect in my htaccess file:
Redirect 301 /foobar /johndoe/foobar
unfortunately, this url:
/foobar/barfoo also gets redirected - why? In my understanding, only /foobar should be redirected when using the Redirect command, shouldnt it?
I feel that this is more comfy than writing RewriteRules

As it turns out, Redirect seems to just check if the URL to check is in the beginning of the current path (at least my tests say that, the documentation is not 100% clear about it).
But, to avoid using RewriteRule (since it might be overkill), simple RedirectMatch also works:
RedirectMatch 301 "^/foobar$" "/johndoe/foobar"
I would still be thankful for additional advice, whether this isnt possible to solve without "regex" and/or RewriteRule

Related

Htaccess redirects with parameters

I am trying to migrate a shop to another system, and would like to redirect my directories.
E.g. www.oldshop.eu/stuff to www.newshop.eu/stuff/
That I do by using
redirect 301 /stuff/ www.newshop.eu/stuff/
That works well, however my current shop has pages of the directories indexed, like:
www.oldshop.eu/stuff/?p=2
That I dont want to transfer to the newshop, however I can see on search console that this is being done. Seems my redirect takes everything after the /stuff/ and just putting it over?!
How can i avoid this so that all url with ?p= or other parameters are being avoided?
Br. Brian
You can use the rewriting module here:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old\.example\.com$
RewriteRule ^/?stuff/?$ https://new.example.com/stuff/ [QSD,R=301,L]
PS: It is a good idea to start out using a R=302 temporary redirection and only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues on the client side.

301 Redirect with cet= parameter

I would like to make a 301 redirect of a url in http://website.com/example-old/?cet=3132 format to a page https://www.website.com/example-new/.
I have tried several times via .htaccess with the classic method:
redirect 301 /example-old/?cet=3132 https://www.website.com/example-new/
The redirect works, but I don't get what I want. In fact the final url becomes https://www.website.com/example-new/?cet=3132.
In short, the ?cet=3132 doesn't disappear and this is not good for me.
I found this invaluable resource which recommends a mod_rewrite method: 301 redirect for old urls with language parameter
Sure I'll try it, but I was wondering: will it work even with the cet=3132 parameter?
Thanks to those who can answer, best regards.
Sure you can use the rewriting module for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cet=3132$
RewriteRule ^/?example-old/?$ https://www.example.com/example-new/ [QSD,L,R=301]
The QSD flag will take care to remove the query string during the redirection.
It is a good idea to start out with a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. That prevents nasty caching issues.

Redirecting A File Anywhere In The Hierarchy

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]

301 redirect throws an error saying the page being redirected does not exist

i want to redirect from a page at /roofing/bellevue/index.php to /bellevue-roofing.php I entered the following:
Redirect 301 /roofing/bellevue/index.php http://www.emeraldstate.com/bellevue-roofing.php
into .htaccess in the root directory.
The result of entering www.emeraldstate.com/roofing/bellevue/index.php is:
The requested URL /roofing/bellevue/index.php was not found on this server.
I have checked and rechecked various sources on formatting Redirects and everything seems correct. Can anyone provide a little guidance?
The correct implementation is to avoid specifying the domain name as part of the redirect. Whilst this will more than likely make no difference to your setup, I'm mentioning it anyway.
You'll be better off using RedirectMatch or mod_rewrite (which I prefer):
RedirectMatch 301 ^/roofing/bellevue/index.php$ /bellevue-roofing.php
Or use mod_rewrite (make sure that the extension is enabled, which it generally is):
RewriteEngine On
RewriteRule ^/roofing/bellevue/index.php$ /bellevue-roofing.php [R=301,L]

301 redirect using regular expression in .htaccess

I have suddenly been hit with hundreds of 404 crawl errors to pages which must have been on a previous site (though I thought I'd got them all...). It's a bit strange as I've never seen any page with index.php in it, yet all the errors start with index.php/xxxxx.
So, I want to do the following:
redirect 301 index.php/<wildcard> http://www.example.com
in the .htaccess file.
Can someone tell me whether this is correct, and what I have to put in the <wildcard> place if it is? If this is incorrect, what is the code to accomplish this?
You can use a the Redirect directive in the htaccess file and do a simple regular expression assuming your site doesn't use /index.php/query/parameters like some PHP frameworks do.
Redirect 301 /index.php/(.+) http://www.mysite.com

Resources