homepage Welcome to WebmasterWorld Guest from 54.166.53.169
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

    
.htaccess redirect inside path.
chetanmadaan



 
Msg#: 4667625 posted 4:13 pm on May 1, 2014 (gmt 0)

Hi -

we have a website which has been setup not being in the root directory and the path is as follows.

www.website.org/site/

Now we want to move everything in the root and outside the /site/ folder.. (Which is fine... I can just move files around)... But keeping in mind that we want to have a wildcard redirect for any traffic coming to the /site/ directory to redirect to the main website.

for instance..

users coming to:
www.website.org/site/somepage/somethingelse.html

should automatically be redirected to:

www.website.org/somepage/somethingelse.html

any help?

 

lucy24

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



 
Msg#: 4667625 posted 6:12 pm on May 1, 2014 (gmt 0)

So your existing htaccess says something like

RewriteRule ^(.+) http://www.example.com/$1 [R=301,L]

(or mod_alias / Redirect-by-that-name equivalent)
with no infinite loop because the htaccess isn't in the root?

Why can't you simply add the directory name to the front of the pattern, outside of the capturing parentheses? That's assuming the former directory will cease to exist, so everything gets redirected in parallel. Is there something more involved?

chetanmadaan



 
Msg#: 4667625 posted 6:27 pm on May 1, 2014 (gmt 0)

@lucy24

currently, there is no htaccess file at all. The website is a static HTML site and there have been no Rewrite rules at all.

I will give it a try and get back here...

lucy24

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



 
Msg#: 4667625 posted 9:13 pm on May 1, 2014 (gmt 0)

Heh. htaccess existed long before there were CMSs that relied upon wholesale rewriting. How do you currently redirect, block, handle cookies, set headers, declare file encodings, give information about removed pages-- and all the other stuff that might be in an htaccess?

There's probably more. Those are all the things I could think of off the top of my head.

chetanmadaan



 
Msg#: 4667625 posted 9:23 pm on May 1, 2014 (gmt 0)

Those are really a lot.

like i said... Site is in Static HTMl... which means it was build around 6 - 8 years ago.

HTML... right?

No expired headers.
No redirects as of right now (May be never needed them).
No login so No cookies.
No 404 or 500 redirects.

:( sorry.. (facepalm)

carminejg3

5+ Year Member



 
Msg#: 4667625 posted 10:45 pm on May 1, 2014 (gmt 0)

You need to know what webserver you are using then use the correct rewrite code.

you can also make a file in notepad, name it .htaccess then insert this code into it.


RewriteEngine on
RewriteRule ^(.+) http://www.example.com/$1 [R=301,L]


As a side note, You can even put that redirect code into your Apache httpd configuration file.

Using .htaccess files really slows down a server, since the server is required to look in EVERY folder for an .htaccess file. But if you aren't running a high traffic site right now htaccess saves a lot of time

Also if you are using apache web server:

[httpd.apache.org...]

lucy24

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



 
Msg#: 4667625 posted 11:04 pm on May 1, 2014 (gmt 0)

RewriteEngine on
RewriteRule ^(.+) http://www.example.com/$1 [R=301,L]

I gave this form in my first post because it illustrates something that can only work if the htaccess file isn't in the root. (Except that the ^ isn't needed: that was a brain fart on my part.) Or, of course, if you're redirecting globally to some other site. Otherwise it would be an infinite loop.

Do you have access to the config file? If yes, there would be no reason for an htaccess in the long term, though it's sometimes useful when you've introduced major changes. If no, I'm still wondering how you deny access to Ukrainian robots :)

chetanmadaan



 
Msg#: 4667625 posted 11:09 pm on May 1, 2014 (gmt 0)

Thanks luck24 and carminejg3... This worked out fine and i am able to direct all traffic.

the only problem... it not redirecting for the index page.

for instance
These are fine
www.website.org/site/somepage/somethingelse.html
www.website.org/site/somepage/
www.website.org/site/some.html

but this is not
www.website.org/site/

carminejg3

5+ Year Member



 
Msg#: 4667625 posted 11:20 pm on May 1, 2014 (gmt 0)

mind trying this? the RedirectMatch is suppose to redirect root level access

RewriteEngine on
RewriteRule ^(.+) http://www.example.com/$1 [R=301,L]
RedirectMatch ^/$ http://www.example.com/site/

lucy24

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



 
Msg#: 4667625 posted 12:37 am on May 2, 2014 (gmt 0)

Replace the + with * for "zero or none". In htaccess-- or <Directory> sections in config-- a request for the front page comes through as null.

But wait. I thought you said you were putting the htaccess in the root? (Normally where it belongs.) If so, requests will never be empty, because they'll always have the leading /directory/. The capture might be empty, but not the whole request.

Incidentally, you can do some two-birds-with-one-stone business here, because the rule could be expressed as

RewriteRule ^directory/(index\.html)?$ http://www.example.com/directory/ [R=301,L]

concurrently getting rid of the "index.html". Even if you've been careful never to give this form in links, search engines will ask for it. So you may as well redirect them in one step.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4667625 posted 12:57 am on May 2, 2014 (gmt 0)

chetanmadaan, you should post the contents of your current .htaccess and verify its location.

IMPORTANT: Please Use example.com (or .org) For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]

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