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]
Related
My application webroot is like : /home/username/appname/public_html
I have created a subfolder under public_html and installed the wordpress there.
Like: /home/username/appname/public_html/subfolder
we used the below redirection code because we want to access the application like https://example.com/subfolder
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/br/$1 [L,R=301,NC]
RewriteRule ^/?(.*)$ http://example.com/br/$1 [L,R=301]
so when user will access main domain example.com so req will be redirected to example.com/subfolder/
but now we want to access example.com/file.txt
but the problem is due to redirection it also redirecting
so how can I exclude this url example.com/file.txt from redirection to example.com/subfolder/file.txt
Please help us.
I tried to add these config but did not work for me
RewriteCond %{REQUEST_URI} !^/file\.txt$
OR
RewriteCond %{REQUEST_URI} !^example\.com\/file\.txt$
I am in need of help with build an htaccess file. Basically I am moving an website from www.abc.com to www.xyz.com. Now, here is the two things that I am trying to achieve:
All users should be redirected to www.xyz.com when accessing www.abc.com
If user accesses www.abc.com/files/abcd.file or www.abc.com/files/folder/abcd.file, etc. should be redirected to arhive.xyz.com/abcd.file, etc. Basicly, this URL www.abc.com/files/, should be replaced with arhive.xyz.com, keeping the same file/directory structure.
Someone can help? Thanks in advance.
You have several requirements, but if I understand correctly this should get what you need in the htaccess file in the root directory.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteCond %{REQUEST_URI} !^/files(/.*)?$ [NC]
RewriteRule ^ http://www.newsite.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.com [NC]
RewriteCond %{REQUEST_URI} ^/files(/.*)?$ [NC]
RewriteRule ^ http://archive.newsite.com%{REQUEST_URI} [R=301,L]
Of course change to your domain names.
I have a website on a domain that I want to move to another domain. Here is my problem. The root URL has to redirect to a different location on the new domain. Example:
http://subdomain.olddomain.com/ must redirect to http://www.newdomain.com/location
http://subdomain.olddomain.com/code/slug must redirect to http://www.newdomain.com/slug
The htaccess code that I have doesn't work ver
RewriteRule ^/?(.*) http://www.newdomain.com/$1 [R=302,L]
This code works for the second example, but nog on the first. Can anyone help me setup the correct htaccess code?
Try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^code/(.*)$ http://www.newdomain.com/$1 [L,R]
RewriteCond %{HTTP_HOST} ^subdomain\.olddomain\.com$ [NC]
RewriteRule ^$ http://www.newdomain.com/location [L,R]
So, this is my .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.mydomain.com/folder/$1 [R=301,L]
The idea was to have www forced upon each user, no matter if the user visits my domain with or without www and at the same time, that user needs to be redirected to "folder" or in other words to www.mydomain.com/folder .
Although this code works perfectly, I want it also to redirect users to www.mydomain.com/folder when they go anywhere on mydomain.com domain except /folder. For example: (www.)mydomain.com/index.html (or any other page) and (www.)mydomain.com/anyotherfolder needs to be redirected to www.mydomain.com/folder
Thank you.
Add this additional rule after your existing rule:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule !^folder http://www.mydomain.com/folder/ [R=301,L,NC]
I have a domain name with wildcard dns active. what i want to write in htaccess is that when the user type abc.domain.com/news/news-details.php then it should send request to the file news.php with arguments val=abc.
I have in the htaccess the following code:
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.domain\.com
RewriteCond %{REQUEST_URI} ^news/news-details\.php$
#RewriteCond %{QUERY_STRING} !^id=.
RewriteRule (.*) /news.php?div=%1 [L]
But this is throwing a 404 error. what is the core part are the following two lines to explain
request for www.domain.com/news/news-details.php goes to www.domain.com/news.php
and abc.domain.com/news/news-details.php goes like www.domain.com/news.php?val=abc
Please note that i am not redirecting. the urls will show as a dynamic sub domain. physically there will be no folder. all files are located in the root.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule ^news/news-details.php$ /news.php?div=%1 [NC,QSA,L]