Forum Moderators: phranque

Message Too Old, No Replies

hide part of url

         

newbie080615

1:32 am on Jun 15, 2008 (gmt 0)

10+ Year Member



I made redirect example.com from folder root\ to a subfolder root\example\

My ".htaccess" in \root as below


RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^/?$ http://example.com/example/ [R=301,L]

The url shows as http://example.com/example/...

Question
I'd like to know how to make change in .htaccess and let the url show http://example.com/... without /example/

Thanks a lot!

jdMorgan

2:44 pm on Jun 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need an internal rewrite, not an external redirect. An external redirect tells the client, "The resource you asked for has moved, please ask for it again at this address," and supplies the redirect address. So the client updates its address bar (browser) or database (search robot), and issues a new HTTP request.

By contrast, an internal rewrite simply changes the filepath associated with the requested URL, and serves the request from that new filepath; No communication with the client about the change takes place.

Because your code is in .htaccess, you'll also need to explicitly prevent looping. And because your RewriteCond accepts all possible hostnames, it doesn't do anything can be deleted. I also assume you want to rewrite requests for *all* resources in /root to /root/example, rather than just the index page. If this is not correct, then the code will need to be changed.


RewriteEngine on
RewriteCond $1 !^example/
RewriteRule (.*) /example/ [L]

Jim

newbie080615

11:35 pm on Jun 18, 2008 (gmt 0)

10+ Year Member



Hi, Jim,

Firstly, thanks for your help.

I read some previous post from you, finally understand the difference between external and internal redirect and

 [R=] 
is for external redirect only.

At present, The situation is there have more than one hostname share one hosting space in my case, so I could not rewrite requests for all resources in /root to /root/example.

I read the apache document and try to do some modification in .htaccess by myself, however I did not get the right way and feel so confusing.

could you give a help to do an example to achieve functions as below?

BEFORE

example01.com (Primary Domain) => root\ (as default)
example02.com ( Addon Domain )=> root\example02\ (as default)

AFTER

example01.com => root\example01\www\
www.example01.com => root\example01\www\
forum.example01.com => root\example01\forum\
example02.com => root\example02\www\
www.example02.com => root\example02\www\
forum.example02.com => root\example02\forum\

url shows without ...\example01\www\ ;ect..
Thanks a lot!

jdMorgan

12:08 am on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are two separate parts to this. First, you need to canonicalize your domains. This will simplify the problem you are asking about, and also help your search engine ranking. This should be done before proceeding with the other changes.

Which has higher RageRank and link popularity -- example01.com or www.example01.com?
Which has higher RageRank and link popularity -- example02.com or www.example02.com?

Jim

[edited by: jdMorgan at 12:08 am (utc) on June 19, 2008]

newbie080615

12:57 am on Jun 19, 2008 (gmt 0)

10+ Year Member



According to your instruction, I modified the .htaccess file in root\ which setup an external redirection for my primary domain [example01.com ] to [example01.com ] without effect on its subdomain [forum.example01.com ]


RewriteEngine on
#
########## Start to Canonicalize ##########
Rewritecond %{http_host} ^([^.]+\.com) [NC]
RewriteRule (.*) http://www.%1/ [R=301,L]
########## End of Canonicalize ##########

Then, the same way to each addon domain and theirs mainfolder.

Am i doing right with the code?

Furthermore, how to do the next part?(use flags P to define the internal path? how?)

jdMorgan

2:40 am on Jun 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, I think you'll find that with that code, a request for foros.example01.com will be redirected to www.foros.exmple01.com, which will redirect to www.www.foros.exmple01.com, and then to www.www.www.foros.exmple01.com, etc. until a subdomain is requested for which you do not have DNS defined, or until the client or server gives up.

This might work better:


RewriteEngine on
#
# Redirect non-canonical to canonical domains
# Skip next two rules if hostname is canonical
RewriteCond %{HTTP_HOST} ^(www¦forums)\.example01\.com$
RewriteRule .* - [S=2]
#
# Else if non-canonical "forums" subdomain, canonicalize it
RewriteCond %{HTTP_HOST} forums\.
RewriteRule (.*) http://forums.example.com/$1 [R=301,L]
#
# Else default to canonical www.example01.com hostname
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Now that the hostnames are canonical, your list of required rewrites shrinks to:
www.example01.com => root\example01\www\
forum.example01.com => root\example01\forum\
www.example02.com => root\example02\www\
forum.example02.com => root\example02\forum\
which can be done with one rule:

RewriteCond $1 !^(www¦forum)/
RewriteCond %{HTTP_HOST} ^(www¦forum)\.example01\.com$
RewriteRule (.*) /%1/$1 [L]

Note that the first RewriteCond is needed to prevent creation of an 'infinite' rewrite loop.

Replace the broken pipe "¦" characters in the patterns above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim