301 redirect using regular expression in .htaccess - .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

Related

Redirect a Rewrite URL Address

Adding this to my .htaccess file works to shorten the URL and get rid of the .shtml bit.
RewriteEngine On
RewriteRule about about.shtml
But, the original address http://xxx/about.shtml also still exists. I don't want Google or anyone to see that one, so I tried redirecting it to the rewritten URL:
Redirect 301 /about.shtml /about
This gives an error on Firefox that it's not working and on Google that it is being redirected too many times. Maybe it's going in some sort of a loop.
I don't want the original file to show up anywhere in the web address (with the.shtml ending), so not sure what else besides a redirect to do.
Thanks!

Why does this redirect apply?

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

How to achieve a specialized redirect

I have an odd use for htaccess redirect. I have an affiliate system that uses compiled code to produce it's output. (I hate compiled php by the way as you can't fix the developers goofs).
In some cases the prefix to the theme directory is like this:
html/themes/xblue/....
which works, but in some cases it's like this:
html/themes//....
Leaving out the word "xblue", but otherwise correct.
I need to be able to redirect html/themes// to html/themes/xblue/
I've tried the following redirect statements:
# Redirect 301 /affiliates/includes/html/themes// /affiliates/includes/html/themes/xblue/
# Redirect 301 // /xblue/
Neither one works, but gives an infinite number of redirects and breaks the page.
Do you know how I can redirect "themes//" to "themes/xblue/" ?
Try this rule with negative lookahead as your very first rule:
RedirectMatch 302 ^(.*/themes)/(?!xblue/)(.*)$ /$1/xblue/$2
Make sure to clear your browser cache before testing this rule.

Setup htaccess not to redirect when hit from main site

I'm not even sure how to ask this correctly so if I am duplicating a question I apologize. How do I use my htaccess file to only redirect when someone is coming in on something other than the main site name?
Example:
I do not want redirect on www.examplesite.com
I do want to redirect on www.examplesite.com/page.php
I think this is what you're looking for:
Perform a redirection with .htaccess
The easiest and simplest way of redirecting with .htaccess is to use the Apache module mod_alias and its command Redirect. Here’s is how to make a temporary redirection with htaccess:
Redirect /page.php http://www.examplesite.com/go_to_this_page.php
Is this along the lines of what you're asking? If so, I hope it can help.
The Structure : redirect accessed-file URL-to-go-to
The code :
Redirect 301 / http://www.examplesite.com/page.php

Redirect every incoming request to specific path preserving file and query?

As I'm not strong with apache could someone point me in right direction with this?
I currently have urls like this
www.domain.com/public/my_file.php?query=thing&another=thing
What I'm looking to do is to rewrite my code so i it don't use /public/ part anymore, but after that i still need to support all crawlers and old urls people are linking to.
So how would i do 301 redirect preserving everything that comes after public/ part?
(Example) Need to redirect something like this
www.domain.com/public/my_file.php?query=thing&another=thing
into this
www.domain.com/my_file.php?query=thing&another=thing
with 301 redirect.
Thnaks.
Redirect 301 /public/my_file.php /my_file.php
The query string gets passed along by default.
EDIT:
To redirect everything in the public folder, use this:
RedirectMatch 301 /public/(.*) /$1

Resources