: Rewrite subdomain to folder keeping the URL unchanged I'm on a cheap shared hosting with Apache and I'm trying to do the following rewrite with .htaccess: http://anything.example.com/par1/par2 =>
I'm on a cheap shared hosting with Apache and I'm trying to do the following rewrite with .htaccess:
anything.example.com/par1/par2 => example.com/anything/index.php?a=par1&b=par2
To do this I have the following .htaccess file in the root directory:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ example.com/%1/ [L,NC,QSA]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /index.php?section=&page= [L]
And this works actually. The problem is that if I put digitalcameras.example.com/nikon/123 to the web browser address string and press Enter, the string becomes like this:
example.com/digitalcameras/nikon/123
(Note that the digitalcameras part moved from the beginning to the middle).
index.php placed in the /digitalcameras folder.
So the question is: can I do the same thing without changing the address in the browser?
More posts by @Sue5673885
1 Comments
Sorted by latest first Latest Oldest Best
RewriteRule ^(.*)$ example.com/%1/ [L,NC,QSA]
When you specify an absolute URL (scheme + hostname) in the RewriteRule substitution it will implicitly trigger an external redirect - which is what you are seeing.
However, if the subdomain(s) and main domain are all on the same shared hosting account (ie. the same filesystem) then you shouldn't have to explicitly specify the full domain in the susbstitution, just specify the file system path.
For example:
RewriteRule (.*) /%1/ [L]
The NC and QSA flags would seem to be superfluous here. The query string is appended by default, unless you explicitly include a query string in the substitution. And the pattern anchors (^ and $) are not required either when using a match everything .* pattern.
UPDATE#1: The first RewriteRule would seem to be superfluous. This is a wildcard subdomain, so I assume the subdomain points to the same document root as the main domain.
What you are really trying to do is internally rewrite anything.example.com/par1/par2 to /anything/index.php?a=par1&b=par2 on the same subdomain.
Try the following instead:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC]
RewriteRule ^([^/.]+)/([^/.]+)/?$ %1/index.php?section=&page= [L,QSA]
The QSA flag is required here, if the original request (pretty URL) could have a query string, which needs to be passed through.
No need to escape the dot in a character class in order to match a literal dot.
The RewriteCond directive checking against the REDIRECT_STATUS environment variable prevents a potential rewrite loop (which would result in a 500 error in the browser).
UPDATE#2: To avoid the explicit use of the domain (eg. example.com) then you could just match the first domain segment, ie. upto the first dot. For example:
RewriteCond %{HTTP_HOST} ^([^.])+
However, this would also match the main apex domain (or www), unless you already have a directive to block that. I assume accessing the main domain like this would strictly be invalid? However, this might be OK if your script handles such an invalid request with a 404?
Or, to match only subdomains of the main domain, except for www then try:
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([^.])+.[a-z-].(com|co.uk|net) [NC]
You would need to add the TLDs as required (maybe that could be generalised further?).
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.