Welcome to WebmasterWorld Guest from 54.198.87.238

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect with .htaccess

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

WebmasterWorld Senior Member 5+ Year Member



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?
9:52 pm on Aug 23, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
1:35 pm on Sep 4, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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?
6:10 pm on Sep 4, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



You'll need a RewriteRule for the redirect and a preceding RewriteCond to specify the requests that should not be redirected.
7:08 pm on Sep 4, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
9:47 pm on Sep 4, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
5:36 am on Sep 5, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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 :)
2:09 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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
7:57 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
8:10 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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
9:24 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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.
10:38 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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]
11:25 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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!
11:43 pm on Sep 17, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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 :)
12:06 am on Sep 18, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Hee.

phranque, you can now bring out the scissors and do the Thread Tidy thing. Careful not to leave any ghosts :)
2:08 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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?
5:22 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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.
6:19 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
7:25 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
7:33 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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.
10:44 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
10:57 pm on Sep 19, 2013 (gmt 0)

WebmasterWorld Senior Member Top Contributors Of The Month



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.
12:37 pm on Sep 20, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month