I have this folder structure
webroot/
www/
srv/
When I visit http://www.domain.com, it points to the www/ folder using this codes:
RewriteCond %{HTTP:Host} ^(?:www\.)?domain\.com$
RewriteRule (.*) /www/$1 [NC,L,NS]
It shows the contents of the www/ folder without the folder name on the address bar. It retains its name to:
http://www.domain.com
What I want is to use the .htaccess mod_rewrite to change from:
http://sub.domain.com
to
http://sub.domain.com/srv
Where it shows the sub folder and it contents. Is it possible? I tried:
RewriteCond %{HTTP:Host} ^(?:sub\.domain\.com)?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/srv/$1 [R=301,L]
but it returns a redirect loop error. How can I do this?
You need to check that it isn't already pointing to /srv. Try:
RewriteCond %{HTTP:Host} ^(?:sub\.domain\.com)?$
RewriteCond %{REQUEST_URI} !^/srv/
RewriteRule ^(.*)$ http://%{HTTP_HOST}/srv/$1 [R=301,L]
Related
I have a following sub domain and folder structure
https://subdomain.maindomain.com/companyName1/area/dashboard
I want to mod_rewrite to this
https://subdomain.maindomain.com/companyName1/area/
So that everything in "dashboard" loads, in "area"
I would like the .htaccess file to live in the area folder, as there are multiple companyNames
I've tried placing the following .htaccess in companyName1/area
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/dashboard/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /dashboard/$1 [L]
However when visiting https://subdomain.maindomain.com/companyName1/area/ it just loads the index.html of that folder, and not of the /dashboard folder
With your shown samples, attempts please try following htaccess rules. Please make sure to keep these rules at the top of your htaccess file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /companyName1/area/
##For external redirect to area link.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s.*/dashboard/?\s [NC]
RewriteRule ^(.*)/dashboard/?$ /$1/? [R=301,L]
##For internal rewrite to dashboard folder here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
what I want to achieve is my domain.com will use the subdirectory /new as the root document folder but when I tried visiting the site it redirects me to domain.com/new which is not what I want
the script was working before, but after i edit the script to use a different folder it doesn't work as expected.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain-name.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain-name.com$
RewriteCond %{REQUEST_URI} !new/
RewriteRule (.*) /new/$1 [L]
it redirects me to domain.com/new. i dont want /new to be shown in the url. thank you in advance!
Change your rules with this rewrite
RewriteEngine on
RewriteBase /new/
My server has several system folders and files by default. so for better overview I want to put my website in a subfolder like this:
www.domain.de/web/index.html
ALL links and website-related stuff is found in this subfolder "web". BUT the folder "web" should not be seen in the url!
I got this with these lines in my htaccess and it works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1
But now the problem:
I also want other folder on my server to work. So www.domain.de/downloads/ should work, too. This folder is NOT in the subfolder "web". So if I write the direct url "www.domain.de/downloads" in my webbrowser, I don't want to get a redirect to "www.domain.de/web/downloads/", you know?
Is there any solution? :)
Thanks!
You Can use a RewriteCond to exclude your folder :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/download [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1
I want to to redirect access to mysite.com/mydir/, to mysite.com/mydir/index.html
There is a .htaccess config file inside the mydir but it doesn't work as expected:
RewriteCond %{REQUEST_URI} ^/
Rewriterule ^(.*)$ index.html [L,R=301]
Now when I go to mysite.com/mydir/ , I am redirected to:
mysite.com/mydir/C:/wamp/www/mysite/index.html
and If use instead:
Rewriterule ^(.*)$ /index.html [L,R=301]
I am redirected to
mysite.com/index.html
Only thing that works is:
Rewriterule ^$ /mydir/index.html [L,R=301]
but I don't want to hardcode mydir, because this .htaccess might be used in another directory. Could this be solved in a modular way?
You can try with the following (.htaccess file stored in mydir):
RewriteEngine on
RewriteBase /mydir/
RewriteCond %{REQUEST_URI} ^/mydir/$ [NC]
RewriteRule ^ index.html [R=301,L]
I haven't been able to test this since I don't have apache installed locally, but the problem is that you are not appending the group in the parens to the start of the url, if you try
Rewriterule ^(.*)$ $1/index.html [L,R=301]
Then it should will prepend what ever the characters at the start of your URL are i.e. mydir/ before index.html
Try this
RewriteCond %{REQUEST_URI} /$
Rewriterule (.*) http://%{HTTP_HOST}%{REQUEST_URI}index.html [L,R=301]
I have a bunch of domains which points to the same directory "public_html" on my host.
There is a .htaccess file in the main folder that locally redirects each domain to a particular folder with the same name as %{HTTP_HOST} mod_rewrite variable. (eg: redirect www.domain.com to public_html/www.domain.com/)
This is the content of the .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond /%{HTTP_HOST}/#%{REQUEST_URI} !^([^#]+)#\1
RewriteRule ^(.*)$ %{HTTP_HOST}/$1 [L]
I'm having a problem when it comes to the Directory Slash directive.
If I try to access a folder in the domain url without the forward slash like http://www.domain.com/folder, the mod_dir applies the DirectorySlash and externally redirects my request to http://www.domain.com/www.domain.com/folder/
I tried applying a 301 redirect AFTER the domain directory redirect like this:
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]
But for this to work, I would have to be able to check if the %{REQUEST_FILENAME} exists inside the %{HTTP_HOST} 'folder'. I even tried the following, for no success:
RewriteCond /%{HTTP_HOST}/%{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]
How can I check, Dynamically, if the %{REQUEST_FILENAME} is a directory, which would be inside a directory with the same name as %{HTTP_HOST}?
Thanks in advance
If you work with -d you need to provide an absolute filesystem path. So try this:
RewriteCond %{DOCUMENT_ROOT}/%{HTTP_HOST}%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ $1/ [R=301,L]