We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

htaccess redirect wildcard

bakum
bakum asked
on
Medium Priority
4,643 Views
Last Modified: 2008-01-09
Hi, little confused on how wildcards work in htaccess redirects....

redirect 301 /abcmag/*.* http://www.abc.com

how do I represent the *.*?  Also, does that *.* work for subdirectories to?  Like /abcmag/dir1/index.php AND /abcmag/dir2/testing.php ?  I want it to redirect everything from /abcmag on down to the new address.

thanks!

-mb
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Try something like this in the htaccess in your abcmag folder:

RewriteEngine ON
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=301,L]

Should map everything to your new site, so if somebody tries to access /abcmag/dir1/index.php, it will send them to http://www.abc.com/dir1/index.php.

Hope that helps :)
CERTIFIED EXPERT

Commented:
Sorry, forgot to mention that the above example may not work if your server doesn't have mod_rewrite enabled in Apache.  This is usually turned on, but I don't know what kind of server/hosting you have.  There's a chance it may not work for you.
Redirect /olddirectory http://yoursite.com/newdirectory/
This will redirect everything inside the olddirectory to your new directory. So, if you are using
Redirect /olddirectory http://yoursite.com/
Then http://youroldsite.com/olddirectory/dir1 will now be http://yoursite.com/dir1
and http://youroldsite.com/olddirecoty/dir2 will now be http://yoursite.com/dir2 and so on.
You do not need to use *.* It is already implied if you are not specifying a file name. Hope that helps.
CERTIFIED EXPERT

Commented:
If it's a permanent move, you NEED to be using a 301 redirect so that search engines realize that it's permanent.  Otherwise, it will send a 302 status code, which means temporary redirect.  So if you're not going to use the mod_rewrite method (or can't use it) then do this:

Redirect 301 /abcmag http://www.abc.com/

Author

Commented:
OK, we're close...

Redirect /olddirectory http://yoursite.com/   - RIGHT
Then http://youroldsite.com/olddirectory/dir1 will now be http://yoursite.com/dir1 - no, redirect to  http://yoursite.com
and http://youroldsite.com/olddirecoty/dir2 will now be http://yoursite.com/dir2 - ditto

the two servers do not have similar structures (I didn't do it, I just have to clean up after it, if you know what I mean).  So every file and subfolder of olddirectory/ needs to be redirected permanently to http://abc.com with NO matching director structure on the new site.  

Cool?  Thanks guys.  I love this site so much!
CERTIFIED EXPERT

Commented:
Try:

Redirect 301 / http://www.abc.com
CERTIFIED EXPERT

Commented:
Or, actually, it might be:

Redirect 301 http://www.abc.com/
CERTIFIED EXPERT

Commented:
Actually, I'm not sure if you can redirect everything to one page using the standard Redirect 301 method.  I think you may need to use a mod_rewrite method, such as:

RewriteEngine ON
RewriteRule /.* http://www.abc.com/ [R=301,L]

Which should send any requests (regardless of page or directory) to http://www.abc.com.
CERTIFIED EXPERT

Commented:
Redirectmatch might work as well (which is not mod_rewrite, in case you can't use that):

redirectmatch permanent (.*) http://www.abc.com/

I think it will still output the 301 code as well using this method.  See which one works best for you and let me know if you still have trouble getting them to work :)
RewriteEngine on
RewriteRule (.*) http://www.yoursite.com/
If you have your Rewrite module installed.

Author

Commented:
OK, so I tried this:

RewriteEngine ON
RewriteRule /olddirectory/.* http://www.abc.com/ [R=301,L]

and it's not working.  www.abc.com/olddirectory/blah.php is redirecting to www.abc.com/olddirectory/blah.php, not www.abc.com/(index.php) like I want it to.  Still a little stumped.  

If it means anything this is a Mambo site.  I don't really have much to do with it other than being asked to fix the things that the Mambo guys can't figure out.  Not really earning my pay today I guess.  Anyway, I mention this because there is only one .htaccess file that I know of, which is why I'm puting the directory in the file path.  Don't know if that makes a difference or not.

-mb

Author

Commented:
also, upping the points as this seems like a harder question than I thought.
CERTIFIED EXPERT

Commented:
Did you try putting this in an htaccess file in the document root of the site you are redirecting?

RewriteEngine ON
RewriteRule /.* http://www.abc.com/ [R=301,L]

Try that and don't change anything other than www.abc.com (to whatever the new site is).
RewriteEngine on
RewriteRule (.*) http://www.yoursite.com/
should be sufficient as I mentioned earlier. Use this inside your document root (the /directory) and then everything in there is redirected to your new website.

Author

Commented:
RewriteEngine ON
RewriteRule /.* http://www.abc.com/ [R=301,L]

This was so CLOSE!  It works, but it's a Mambo site and it actually redirects all the CSS calls too so the site ends up with no styles (which is bad :) ), which is maybe the product of the redirect being a touch too broad?   I'm still nto sure how these expressions are set up, but to reiterate in English (that is to say, not in code):

I want to redirect www.abc.com/abcmag/*.* (and all possible sub dirs and files) to www.abc.com/index.php.  note just the /abcmag/ dir on down gets the redirect.  BUT, the tricky part is this is a mambo site, and there is no actual /abcmag/ dir to plop an htaccess file in, there is only one htaccess file at the root.  What a pain, huh?

Go experts Go!

-mb

CERTIFIED EXPERT
Commented:
What type of files will you be redirecting?  Are they all the same type?  For example .html files or .php files.

I think you could do this:

RewriteEngine ON
RewriteRule /.*html http://www.abc.com/ [R=301,L]

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Try this code:

RewriteEngine ON
RewriteRule /.* http://www.abc.com/ [R=301,L]
RewriteCond   %{REQUEST_URI}  !^/SomeDir/.*
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|js|css)$

Please change "SomeDir" (code line 3) to any directory you do not want to redirect. You can make copies of that line to accomodate more directories. If you don't have any then you can simply delete that line. Code line 4 will take care of your images, css, javascript files. You can add more extensions by using a | delimiter. They will not be redirected. Hope that helps.

Author

Commented:
The user's request was cancelled so this issue will remain unsolved.  Anyway, I split the points between the two of you for some great answers and for hanging in there.  Thanks so much!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.