Been trying to Rewrite wildcard subdomain to a subdirectory and not having allot of luck.
ie
abc.domain.com > www.domain.com/sub/abc
I have tried the folowing
// example 1
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
// example 2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(.*).domain.com [NC]
RewriteRule ^(.*)$ ./sub/$1 [L]
Hope someone can advise.
You need to use backreference from RewriteCond:
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^((?!(?:sub|index\.php)/).*)$ ./sub/%1/$1 [L,NC]
Related
I've been breaking my head over this one for almost 2 hours now.
I have a website (let's call it example.com)
And I need to rewrite this url:
example.com/plant.php?naam=Brahea%20Armata
To
example.com/Brahae_Armata
This is what my .htaccess file looks like atm:
RewriteEngine On
RewriteCond %{HTTP_HOST} example.com/plant.php?naam=$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That doesn't work. Anyone know what the problem is?
You can use these rules in root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\%20(\S*)\sHTTP [NC]
RewriteRule ^ /%1_%2? [NE,R=301,L]
RewriteCond %{THE_REQUEST} /plant\.php\?naam=(.*)\sHTTP [NC]
RewriteRule ^ /%1? [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) page.php?name=$1 [QSA,L]
Scenario
I have 3 domain names:
www.hellokitty.com
www.keroro.com
www.doraemon.com
I want to host all of them in one web hosting with the following directories:
./htdocs/hellokitty.com/
./htdocs/keroro.com/
./htdocs/doraemon.com/
All of them are linked to the same hosting server with A record.
However, my hosting do not support virtual host.
I need .htaccess files to rewrite the URLs
So, I tried in this way:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?hellokitty.com$ [NC]
RewriteCond %{REQUEST_URI} !^/hellokitty\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /hellokitty\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?hellokitty.com$ [NC]
RewriteRule ^(/)?$ hellokitty.com/index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?keroro.com$ [NC]
RewriteCond %{REQUEST_URI} !^/keroro\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /keroro\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?keroro.com$ [NC]
RewriteRule ^(/)?$ keroro.com/index.php [L]
RewriteCond %{HTTP_HOST} ^(www.)?doraemon.com$ [NC]
RewriteCond %{REQUEST_URI} !^/doraemon\.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /doraemon\.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?doraemon.com$ [NC]
RewriteRule ^(/)?$ doraemon.com/index.php [L]
Thus, if I go to www.hellokitty.com, it rewrites to ./htdocs/hellokitty.com/ internally.
Problem
If I go to www.hellokitty.com/hellokitty.com/
Or, if I go to www.hellokitty.com/keroro.com/, it still works
So, my problem is that how to prevent the client direct access the above real URLs and show a 404 error to them?
Insert this rule just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /(hellokitty|keroro)\.com [NC]
RewriteRule ^ - [F]
I am trying to use htaccess to make a subdirectory the root for an additional domain. I currently have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/$1
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteRule ^(/)?$ test/index.php [L]
It works great and hides the 'test' directory in the URL. But, when a file in a subdirectory is requested it shows the 'test' directory. For example, instead of thedrive.co/somefolder it shows thedrive.co/test/somefolder.
How can I get it to not display that?
Insert a 301 rule right at the top below RewriteEngine on line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?radioreformation\.co$ [NC]
RewriteCond %{THE_REQUEST} \s/+radioreformation.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteCond %{REQUEST_URI} !^/radioreformation.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /radioreformation.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteRule ^(/)?$ radioreformation.com/index.php [L]
This will redirect thedrive.co/test/somefolder to thedrive.co/somefolder
I am trying to serve different urls using mod_rewrite but whatever I try it is just not working.
An example url would be
http://www.site.com/country/tours/dynamic-part/?&city=new-york,los-angeles
And I am trying to change the url using .htaccess to:
http://www.site.com/country/tours/dynamic-part/new-york,los-angeles
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/.+city=([^\/]*)$ http://www.site.com/country/tours/$1/$2 [L,R=301]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Any ideas? I though I was close but not anymore :/
The RewriteRule does NOT match the query string, see
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#what_is_matched
So the .+city part of the rule will never match.
This should work tho...
RewriteCond %{QUERY_STRING} (^|&)city=([^&]*)(&|$)
RewriteRule ^country\/tours\/([a-zA-Z0-9]*)\/ http://www.site.com/country/tours/$1/%2 [L,R=301]
The subsitution can read back-referenecs to the RewriteCond pattern.
I want to make a htaccess RewriteRule with a RewriteCond
What I have so far:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I want all requests sent to sql.site.ro to be served from the /ro-site-sql directory.
So if the URI is sql.site.ro/index.php the server will go to /ro-site-sql/index.php, or if URI is sql.site.ro/images/small/9954.png the server will go to /ro-site-sql/images/small/9954.png.
The problem is that if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/$1
I get an Internal Server Error, and if I make
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1
I get Array ( [x] => ro-site-sql/index.php ) inside of /ro-site-sql/index.php. Should it not be just index.php, without the ro-site-sql/ part?
Thanks!
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 ^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sql.site.ro$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(\w+/)+\w+\.\w+$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
This should work to identify actual files and access those and pass anything else to index.php.
The rewrite engine loops over .htaccess files until no more substitutions occur. You need to ensure that this loop stops after the first replacement. There are various ways to do this. Perhaps the easiest to understand is:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(sql.site.ro)$
RewriteCond $1 !index.php$
RewriteRule ^(.*)$ ro-site-sql/index.php?x=$1 [L,QSA]
Add the [L] flag because this is the last rule on this cycle and [QSA] if you also have other parameters sometimes.