Forum Moderators: phranque

Message Too Old, No Replies

htaccess subdomains rewrites

htaccess subdomains rewrites

         

jimmybeige2

10:26 am on Jun 18, 2009 (gmt 0)

10+ Year Member



Hi,
I'm having some difficulty with subdomain rewrites in the htaccess file.
I am trying to rewrite a series of subdomains:

[subdomain.example.com...] to /page1.htm
[subdomain.example.com...] to /page2.htm
[subdomain.example.com...] to /page3.htm

[subdomain2.example.com...] to /page1.htm
[subdomain2.example.com...] to /page2.htm
[subdomain2.example.com...] to /page3.htm

I have gotten the initial rewrite workin with the following:

RewriteCond %{HTTP_HOST} ^(.*)(subdomain)(.*)\.example\.com
RewriteRule ^(.+) %{HTTP_HOST} [NC]
RewriteRule ^(.*)(subdomain)(.*)\.example\.com(.*) page1.htm [L]

But the URL [subdomain.example.com...] seems to also go to /page1.htm rather than /page2.htm as does all the others. How do I ensure that the pages in the subdomain don't all go to /page1.htm?

Thanks

[edited by: jdMorgan at 12:12 pm (utc) on June 18, 2009]
[edit reason] example.com [/edit]

jdMorgan

12:18 pm on Jun 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to rewrite subdomains to unrelated page names, then you will have to write one rule for each subdomain.

If you are willing to rewrite subdomain "apple" to page "page_apple", so that the page name is derived from the subdomain name, then one rule can be used:


RewriteCond $1 !^page_
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^ /page_%1.html [L]

The first RewriteCond here is used to stop a potential 'infinite' rewriting loop.

Be aware that as you have requested, *all* URL-paths in your subdomains will be rewritten to an HTML page. You should reconsider this plan, since all image requests, requests for robots.txt and sitemap.xml files --everything-- will be rewritten to this HTML page.

In order to rewrite only the 'root' of each subdomain, change the rule pattern from "^" to "^$".

Jim

g1smd

1:10 am on Jun 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure this does what you expect it to?

RewriteRule ^(.*)(subdomain)(.*)\.example\.com(.*) page1.htm [L]

jimmybeige2

1:17 pm on Jun 24, 2009 (gmt 0)

10+ Year Member



Hi,
thanks for the reply, that's not working for me however. I really am not that knowledgeable about htaccess rewrites and am just trying to get bits off the web. Basically I will need to do rewrites for a lot of pages and it's not following any real pattern. They are for pages in country subdomains of a site.

ie
[france.mysite.com...]
must go to [mysite.com...]

[france.mysite.com...]
must go to [mysite.com...]

[france.mysite.com...]
must go to [mysite.com...]

[italy.mysite.com...]
must go to [mysite.com...]

I just need to be able to rewrite these to go to the contentpage.php with whatever id I specify. Is this possible? I understand I may need to write lots of seperate rewrite rules for all of these.

Thanks again for the help.

jdMorgan

2:03 pm on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{HTTP_HOST} ^france\.example\.com
RewriteRule ^myrandompagename\.htm$ /contentpage.php?id=1 [L]
...
RewriteCond %{HTTP_HOST} ^italy\.example\.com
RewriteRule ^myrandompagename\.htm$ /contentpage.php?id=4 [L]

If you forsee this site growing, I strongly suggest that you do not continue to add more "randompagename" URLs (and rewrite rules) to the site. Instead, consider incorporating the "id" number in the published URL in the most unobtrusive way possible, and then use one rewriterule to handle all of them.

For example, you could easily use a URL like "art-3-myrandompagename.htm". The only requirements are that the id number be in a fixed position in the URL, that that id number be distinguishable from the "randompagename" part of the URL (done here using the first and second hyphen), and that URLs which need to be rewritten are easy to distinguish from those which must not be rewritten(done here with the "(art)icle" tag at the beginning of the URL).

Both the URL format and the "art" tag shown here in this example are arbitrary and not required; Only the three requirements listed above must be met.

Jim