homepage Welcome to WebmasterWorld Guest from 54.167.185.110
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect with .htaccess
andrewsmd




msg:4604418
 2:21 pm on Aug 23, 2013 (gmt 0)

Ok, I have about 0 experience with apache, I've always ran things on a windows box. I have a site that I need to redirect everything to a different url, minus one folder. I'm guessing I can do this with an htaccess file but I'm not sure how.

Basically I want anything in domain.com/lab to go where it should normally, and then anything else on the entire domain.com domain to go to domain2.com. How would I go about this?

 

lucy24




msg:4604519
 9:52 pm on Aug 23, 2013 (gmt 0)

Well, what have you got so far?

:: insert Why We Make You Do It Yourself boilerplate ::

You'll need a single RewriteRule, plus at least one RewriteCond looking at %{REQUEST_URI}. Details depend on whether both hosts live on the same server, and if so, whether requests pass through the same htaccess, if any.

andrewsmd




msg:4607238
 1:35 pm on Sep 4, 2013 (gmt 0)

Ok, sorry it took me so long to get to this. So I found the website root directory on this server. I put in a .htaccess file with this
Redirect /* http://example.com/

I restarted apache but when I load the old site, it still redirects, any ideas why?

g1smd




msg:4607310
 6:10 pm on Sep 4, 2013 (gmt 0)

You'll need a RewriteRule for the redirect and a preceding RewriteCond to specify the requests that should not be redirected.

andrewsmd




msg:4607321
 7:08 pm on Sep 4, 2013 (gmt 0)

Do you know of a good tutorial on this? I've read a few but that's what I thought I needed from reading them.

lucy24




msg:4607359
 9:47 pm on Sep 4, 2013 (gmt 0)

For starters:

The whole point of htaccess is that you don't need to restart the server. You do need to set
AllowOverride [httpd.apache.org]
to the appropriate values. Link is to 2.2; change to 2.4 if appropriate. If you don't, htaccess won't work, where "won't work" = "will be ignored as if it did not exist at all". If you're testing out new rules, htaccess is useful precisely because you don't have to keep stopping and starting the server.

Anything that can be done in htaccess can be done in the config file. The reverse is not true.

I have about 0 experience with apache, I've always ran things on a windows box.

Does "windows box" here mean IIS? You can run Apache on any OS.

Now then:
You cannot use mod_alias (Redirect by that name). It has to be done in mod_rewrite [httpd.apache.org] (RewriteRule with [R] flag). Change 2.2 to 2.4 if necessary, but differences aren't significant here.

The basic pattern for what you want to do is

RewriteCond %{REQUEST_URI} !/lab
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

Ordinarily I'd look at constraining the rule to requests for pages. But if it's going to a whole different domain, non-page requests will never see the rule in the first place.

That's assuming the target domain lives on a different server and/or is under a different htaccess file. If the new requests pass through the same config or htaccess file, you will need a further RewriteCond looking at %{HTTP_HOST} unless you can put the rule inside a <Directory> section (config only). If you do go with htaccess permanently, put each domain's RewriteRules in a separate htaccess file.

JD_Toims




msg:4607453
 5:36 am on Sep 5, 2013 (gmt 0)

RewriteRule !^lab http://www.domain2.com%{REQUEST_URI} [R=301,L]

I save some characters on Lucy24? Wow, it's not often I get to do that! -- She's usually super crazy efficient :)

andrewsmd




msg:4610317
 2:09 pm on Sep 17, 2013 (gmt 0)

Ok, sorry for the delay, things have been hectic at work. So I have a couple questions for you, sorry if they're stupid ones.

From what I read, do I need to edit a httpd.config file to make sure AllowOverride is on? I couldn't find a file like that anywhere in the apache folders so I'm not even sure how to tell if that's enabled and I can actually use a .htaccess file. I'm thinking no because I put in both of your rules and nothing happened when I'd go to pages, they loaded just like normal.

How would I go about finding that config file for Apache and making sure I have the proper stuff enabled.

Just for reference, this site I need to redirect sits on a windows server running apache (not mine). The new site is on a completely different box running IIS and .net

lucy24




msg:4610376
 7:57 pm on Sep 17, 2013 (gmt 0)

Everything happens on the old site. New site doesn't matter.

The config file exists. If it didn't, Apache would not run. I don't know where it lives on a production server; all I know is that MAMP has a directory called "conf" and this is probably not much help :)

In some obscure directory that doesn't currently have a named index.xtn file, make an htaccess file containing the single line
Options +Indexes
If overrides are permitted, you will now get an auto-index of the directory. If instead you get a 403, you really need to find that config file. (If everything is already auto-indexed by default, make it say -Indexes to test.) This is not a 100% reliable test, because auto-indexing is not the same override category as mod_rewrite. But it's a quick-and-easy thing to try.

andrewsmd




msg:4610379
 8:10 pm on Sep 17, 2013 (gmt 0)

Oh I wasn't saying there wasn't a config file, I was just saying I couldn't find it :). So I'm pretty sure I found the tomcat folder and then the conf within there.

The files in there are context, newserver, server, tomcat-users and web

all xml files. Would one of them be where I'd check to see if htaccess files are enabled?

I looked through the server and web.xml files but didn't see anything that looked related to what I was reading about allowoverride.

I tried putting your text in the htaccess file and the pages within the site still load fine.

What do you think I should look for next? Thanks for the help

JD_Toims




msg:4610388
 9:24 pm on Sep 17, 2013 (gmt 0)

It's easier to just run a test than digging through the server to find out if .htaccess/mod_rewrite is available.

To keep from having a site-wide impact:

1.) Create a test directory on your old site/server, say /123-testing/

2.) Put an .htaccess file in the /123-testing/ directory with the following:

RewriteEngine on
RewriteRule .? http://www.webmasterworld.com/apache/ [R=301,L]

3.) Type your-old-site.com/123-testing/anything.ext into your browser.

If you end up back here let us know it worked. If not, then you'll need to find the config file and make the necessary adjustments.

JD_Toims




msg:4610401
 10:38 pm on Sep 17, 2013 (gmt 0)

I don't see where adding Options +FollowSymLinks to the top of the .htaccess has been tried, so that's probably a good idea to test just to see what we get out of it.

Note: Try the preceding in the test directory's .htaccess again to keep from impacting the whole site and if you receive a 500 server error when requesting your-old-site.com/123-testing/something-else.ext it would seem like it's a permissions issue even though Options All was the default until Apache 2.3 and since then FollowSymLinks has been.

[Edited: I meant Options in the second paragraph but typed AllowOverride the first time -- Glad I got to it before Lucy24 made it by. Not sure how I'd live that one down lol]

lucy24




msg:4610414
 11:25 pm on Sep 17, 2013 (gmt 0)

I tried putting your text in the htaccess file and the pages within the site still load fine.

My suggested test would only apply to one thing: auto-indexing of directories that don't have a named index.xtn file. For example an /images/ subdirectory.

even though AllowOverride All was the default until Apache 2.3 and since then FollowSymLinks

Aaack! Not you too! All vs. FollowSymLinks is Options. All vs. None is AllowOverride.

:: shuffling papers ::

Oh, oops, I'm thinking of a different thread. But it remains true.

Glad I got to it before Lucy24 made it by. Not sure how I'd live that one down lol

ROFL. I don't think we've ever cut it this close!

JD_Toims




msg:4610416
 11:43 pm on Sep 17, 2013 (gmt 0)

ROFL. I don't think we've ever cut it this close!

When I came back and reread my post I *knew* it would be close!

Aaack! Not you too! All vs. FollowSymLinks is Options. All vs. None is AllowOverride.

The only way you would make that statement in this thread is if we were posting at the same time and you saw my previous post pre-edit -- Soooooo glad I can type fast! lol :)

lucy24




msg:4610420
 12:06 am on Sep 18, 2013 (gmt 0)

Hee.

phranque, you can now bring out the scissors and do the Thread Tidy thing. Careful not to leave any ghosts :)

andrewsmd




msg:4610940
 2:08 pm on Sep 19, 2013 (gmt 0)

Ok, putting that in a temp directory did not redirect me. Honestly this site has like 20 page and I'm half tempted to just manually redirect every page at this point. Do you have any other ideas?

JD_Toims




msg:4611009
 5:22 pm on Sep 19, 2013 (gmt 0)

So I'm pretty sure I found the tomcat folder and then the conf within there.

Well the little emphasis I added above tells way more of the story I didn't notice during skimming -- Tomact is a JavaServer and will not have an httpd.conf file.

Honestly, after looking into it a bit and since you only have 20 pages I'd just put a 0 second <meta> refresh on each page and move on -- It might take a bit longer for them to be treated as permanent redirects by search engines, but considering the headache likely from trying to configure Tomcat to redirect I think I'd just be done with it in 15 mins or less and exercise a bit more patience for the redirects to be applied by search engines.

andrewsmd




msg:4611025
 6:19 pm on Sep 19, 2013 (gmt 0)

I'll probably just set up scripted pages to redirect. I don't like metas because they take a second or so and it like flashes a page, then redirects.

lucy24




msg:4611040
 7:25 pm on Sep 19, 2013 (gmt 0)

Honestly this site has like 20 page

Literally 20? Or really more like 200? If it truly is about 20 pages, then heck yes: code a separate explicit redirect for each page. But keep them all in the same htaccess file.

Once you've got your list of 20 exact redirects, you will probably be able to look at them and see which ones can be collapsed into a single rule with appropriate capturing.

JD_Toims




msg:4611046
 7:33 pm on Sep 19, 2013 (gmt 0)

But keep them all in the same htaccess file.

He's on tomcat -- No httpd.conf or .htaccess rewriting available. Neither file is used or available unless Apache's HTTP server is installed "over" tomcat, so redirects have to be done in a totally different way than they would on a "standard HTTP" Apache installation.

lucy24




msg:4611094
 10:44 pm on Sep 19, 2013 (gmt 0)

Whoops! I always had the vague idea that Tomcat ran on top of Apache as an add-on. Flat out backward :(

But 20 files is 20 files. It may really be faster and easier to handle them individually. And we're talking about old URLs, right? So there's no danger that the number of necessary redirects will balloon in the future.

JD_Toims




msg:4611097
 10:57 pm on Sep 19, 2013 (gmt 0)

But 20 files is 20 files. It may really be faster and easier to handle them individually.

I think this is likely true and the best idea -- You're right, 20 files is nothing to redirect individually. I'm fairly certain either of us could put 20 redirects in place in less time than we've spent on this thread and even if the pages do move again changing 20 redirects is faster than putting them in place initially, so meh, I'd redirect individually and be done with it.

andrewsmd




msg:4611277
 12:37 pm on Sep 20, 2013 (gmt 0)

I knew that a while ago, it was more me wanting to figure out how to do it the right way, vs each individual file. It's done now though.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved