I am looking to move http://domain.com/blog to http://blog.domain.com.This also means that everything that trails /blog for example /blog/post/1 will need to be routed to http://blog.domain.com/post/1.
Ensure that you've got content on blog.domain.com. Specifically, if you go to http://blog.domain.com/post/1 you get served the correct content.
In the htaccess file in your domain.com domain's document root, add (preferably above any rules you may already have there):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$ [NC]
RewriteRule ^/?blog/(.*)$ http://blog.domain.com/$1 [L,R=301]
If you actually don't have any content at blog.domain.com and it shares the same document root as domain.com, then you'll need to add these additional rules:
RewriteCond %{HTTP_HOST} ^blog.domain.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/blog%{REQUEST_URI} -d
RewriteRule ^ /blog%{REQUEST_URI} [L]
Related
I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
I want my domain without www always displayed, ie the URL is valid: http://miweb.net And besides this domain to point to a subdirectory and not the root directory.
How I can indicate in the htacess both conditions?
You may want to take a look at VirtualHost.
Assuming you are using Apache, this will allow you to redirect URLs with www or other sub-domains to different folders on the server.
I think I've found the solution. Apparently it works:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?myweb.net$
RewriteCond %{REQUEST_URI} !^/myweb/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myweb/$1
RewriteCond %{HTTP_HOST} ^www\.myweb\.net$
RewriteRule ^/?$ "http\:\/\/myweb\.net\/" [R=301,L]
RewriteRule ^(/)?$ myweb/index.html [L]
If you access from a browser http://www.myweb.net, we automatically redirected to http://myweb.net (no www).
And that domain no longer points to the root directory of hosting "/ www /" if not "/ www / myweb /".
I have this website http://www.foo.com/bar/index.php. I recently moved my site FROM foo.com/bar/index.php TO foo.com/index.php
In my htaccess file, how do i write the conditions so that if any request is made from lets say .foo.com/bar/somedirectory/somefile REDIRECT to foo.com/somedirectory/somefile
In the htaccess file in your document root, preferably above any rules that do routing (to index.php, for example), add these rules:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/bar/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^/?bar/(.*)$ /$1 [L]
If you want to externally redirect the client (changing the URL in the browser's address bar), then add the R flag in the rule's brackets: [L,R=301].
Add below to your foo.com/bar/.htaccess file:
RewriteEngine On
RewriteRule ^(.*)?$ http://%{HTTP_HOST}/$1 [R=301,QSA,L]
I was wondering if it's possible to create a subdomain using htaccess code only,
(Without setting one in the hosting control panel)
I have a folder named "mobile",
It's location is like so: http://www.domain.com/mobile
I want to be able to enter http://mobile.domain.com and see the mobile folder,
Is this possible to achieve only by writing some code in the htaccess?
if so, how do I do that?
Thanks
You can do this as long as it's configured to handle all requests for *.domain.com domains, then just make sure you have mobile.domain.com's DNS CNAME'ed to www.domain.com.
If you know what subdomains you want, you can enumerate through them like so (putting these rules in the htaccess file in your document root):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.) [NC]
RewriteCond %{HTTP_HOST} ^mobile\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mobile/
RewriteCond %{DOCUMENT_ROOT}/mobile%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/mobile%{REQUEST_URI} -d
RewriteRule ^(.*)$ /mobile/$1 [L]
Otherwise, if you wanted to do this arbitrarily, it gets a little more complicated:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.) [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1:%{REQUEST_URI} !^([^:]+):/\1/
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /%1/$1 [L]
Depends on the config of your domains.
If you set them up with a wildcard ServerAlias, like so:
ServerName example.com
ServerAlias *.example.com
DocumentRoot /var/www/example.com/
The request will land in the folder and the .htaccess will be evaluated, enabling you to use mod_rewrite. If you don't specify the ServerAlias directive though, the request will never reach the folder and as such you wont be able to do anything with it.
We just redesigned a site for a client in EE, located at example.com (with and without www.). Their original site is ASPX. They've still got a number of ASPX pages that they want to keep, so their IT people created a subdomain, www2, which is basically a clone of their old site.
I need an htaccess rule that will check if the requested page ends in .aspx, then redirects to the www2 subdomain. It should also make sure that the requested page doesn't exist
I tried using the following rule, but it doesn't work.
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
My htaccess file (including the above rule) looks like this:
RewriteEngine On
# redirect all .aspx pages to www2
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
# strip index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Does anyone have a solution for this?
The RewriteRule directive does only test the URL path. If you want to test any other part of the requested URL, you need to use the RewriteCond directive:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]