Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond with Subdirectories and Domains

         

soundstorm

1:20 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



Hi,

This is my first post. So I hope I do everything right (despite the rant on the bottom). ;)

I need some help for a RewriteCond in htaccess. I have this up and working:


RewriteCond %{HTTP_HOST} ^(www\.)?example-A\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/subdirectory/$1 [L,R=301]


How can I expand the first line to accept subdirectories? I'd like www.domainA.ch/directory984/ -> www.domainb.ch/directory673/ No matter what I try it just routes back to the root (www.domainB.ch). I cannot make it recognize the subdirectory. Any help is very appreciated.

(Off Topic: How do I post http:// without getting it converted to a URL? The code above ist not correct like this on the second line... The "CODE" tag does not work, neither does "QUOTE" or "disable Code/Smilies". And why has the "Code" tag such a small fontsize? Very hard to read.. Isn't it code that gets posted all the time? :) Sorry for the rant on the first post. :))

[edited by: jdMorgan at 1:41 pm (utc) on Jun 16, 2010]
[edit reason] Please use example.com only [/edit]

jdMorgan

1:47 pm on Jun 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can post "http://example.com" or "http://www.example.com" as requested in our Forum Charter, and auto-linking will not be invoked. Otherwise, I must spend time editing your post which otherwise might be used answering your questions...

Your code (as amended) redirects requests for www.example-A.com/<path> to www.example.com/subdirectory/<path>

As such, it should be working, as long as you want that "/subdirectory" path-part to be inserted ahead of the originally-requested subdirectory. If it is not working, then there is some other rule on one of the servers that is interfering with this rule.

Any changes would be to the second line -the RewriteRule- not the first. The HTTP_HOST variable examined by RewriteCond does not contain any URL-path information, it only contains the hostname (possibly in FQDN format with a trailing period) and an optional appended port number -- e.g. "www.example.com.:80"

Jim

soundstorm

2:11 pm on Jun 16, 2010 (gmt 0)

10+ Year Member



Hi jdMorgan

Thank you very much for your answer. I'm sorry that I didn't read the Forum Charter before, I did it now. I could have saved us both a little time.. :)

And thanks for the explanation for HTTP_HOST, I didn't understand this enough. The thing is, the example-A.com Domain is routed to example.com/567/ with the code above. This works. (Example-A.com is an Alias Domain of exaple.com) But additionally I need to "redirect" also subdirectories. And unfortunatly they have not the same/paths, like: example-A.com/123/ -> example.com/567/789/.
And there is also Typo3 Rewrite Engine On.. :(

I see, I must study this more on myself, I'm a beginner on this. But could you give me a hint as to which variable I have to use? Can I use "Redirect" (with a condition) in my situation? Or HTTP_REFERER?

jdMorgan

1:56 pm on Jun 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> And unfortunately they have not the same/paths, like: example-A.com/123/ -> example.com/567/789/

This makes things more difficult. The choices are to write one rule for each differently-named subdirectories, which is feasible only if the number of such subdirectories is small. Alternatively, you can write one (or a few) rules to detect requests for the 'old style' subdirectory URLs, and rewrite all of them to a script, which can then look up the old-to-new URL translation in a database and generate a 301-Moved Permanently redirect. Generally, this latter approach is best when more than a handful of one-off rewriterules would be needed.

Jim

soundstorm

2:20 pm on Jun 17, 2010 (gmt 0)

10+ Year Member



Hi Jim

Thanks so much for your info. I will try the second method.

Have a nice day!
Phil