Welcome to WebmasterWorld Guest from 54.145.208.64

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

.htaccess redirect inside path.

   
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?
6:12 pm on May 1, 2014 (gmt 0)

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



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?
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...
9:13 pm on May 1, 2014 (gmt 0)

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



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.
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)
10:45 pm on May 1, 2014 (gmt 0)

10+ Year Member



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...]
11:04 pm on May 1, 2014 (gmt 0)

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



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 :)
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/
11:20 pm on May 1, 2014 (gmt 0)

10+ Year Member



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/
12:37 am on May 2, 2014 (gmt 0)

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



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.
12:57 am on May 2, 2014 (gmt 0)

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



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]