I am trying to make a subdomain forward through the htaccess document. The below instructions will forward http://sub.domain.com to the internal structure Root > subfolder > index.php. This all works well.
# REWRITE DEFAULTS
# ====================================================================================================
RewriteEngine On
RewriteBase /
# SUBDOMAIN FORWARD
# ====================================================================================================
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1
There is only one (big) downfall with this method. When you try to reach a folder within the subfolder such as Root > subfolder > images by going to http://sub.domain.com/images/ then all works well again, however when you don't end it with a slash http://sub.domain.com/images the url in the address bar will become http://sub.domain.com/subfolder/images/ which is clearly not what I want.
So what if I would add a slash if a slash was forgotten?
RewriteCond %{HTTP_HOST} ^(.*).domain.com$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://%1.domain.com/$1/ [L,R=301]
This all works when you have a normal URL (http://www.domain.com/images), because it will detect it is a directory and adds a slash and if it's a file it will omit the slash. However this does not work for our http://sub.domain.com/images example.
Does anyone have an idea how you can make subdomains work properly using htaccess?
That is happening because /subfolder/images is a real directory and mod_dir module adds a trailing slash to all directories by doing a 301 redirect.
You can use it like this:
RewriteEngine On
# add a trailing slash if /subfolder/images is a directory
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/subfolder/$1/ -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,NE,R=302]
RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteRule ^(.*)$ /subfolder/$1 [L]
Related
I am amending my htaccess file to achieve non-www to www (this is working) plus removing the trailing slash at the end of the URL, e.g.:
www.domain.bc.ca/club/ ---> www.domain.bc.ca/club
www.domain.bc.ca/club/index.html/ ---> www.domain.bc.ca/club/index.html
The portion of the htaccess file is below - the Force www bit is working; the Remove trailing slash bit is not. Help! Many thanks, Amanda.
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
I'm going to go out on a limb and guess that you're trying to access a directory when this happens. In your example, the "club" seems to be a directory and when you redirect /club/ to /club, a module called mod_dir will redirect it back to having the trailing slash again. There's a really good reason for this, because if the trailing slash is missing for a directory, the directory's contents will be displayed instead of the index file. That means if you were able to go to www.domain.com/club (without the trailing slash), you'd see all the contents of the club directory instead of the club/index.html file.
If that's ok with you, then you can turn off mod_dir by adding this to your htaccess file:
DirectorySlash Off
But then you'd need to internally add the slash back in:
DirectorySlash Off
# Force www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^domain\.bc\.ca$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.+)/$ /$1 [R=301,L]
# Add the slash back
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ /$1/ [L]
I've been searching for this but I'm really newbie with .htacces (I find it a hell) and I cannot get the way to solve this.
I have the root folder in my server, redirecting the default domain URL to the folder "web". I have another folder, let's say subfold, and inside this, I have another folder: mobile.
What I want is that if someone types www.adomain.com it goes to root/subfold, without showing the subfold. I've done it this way:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.adomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfold/ [NC]
RewriteRule (.*)$ /subfold/$1 [L]
RewriteCond %{HTTP_HOST} www\.maindomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web/ [NC]
RewriteRule (.*)$ /web/$1 [L]
However, when I go to www.adomain.com/mobile it shows in the URL www.adomain.com/subfold/mobile/
My aim is that if I go to the mobile subfolder, in the URL it should show www.adomain.com/mobile
Obviously, I would like not to have to write a redirect everytime I add a subfolder, it it was possible.
How should I do that?
Thanks for your time and help!
You're getting redirected because your accessing a directory without a trailing slash. The mod_dir module, on by default, will redirect the browser and add a trailing slash if someone tries to access a directory without a trailing slash. This is because there is a information disclosure issue with indexing directories. You can either turn this off:
DirectorySlash Off
and risk having the contents of your directories get listed, even if there's an index.html file. Or add a bunch of rules to ensure there's a trailing slash:
RewriteEngine on
Options +FollowSymLinks
RewriteBase /
# redirect for trailing slash:
RewriteCond %{HTTP_HOST} ^www\.adomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfold/ [NC]
RewriteCond %{DOCUMENT_ROOT}/subfold%{REQUEST_URI} -d
RewriteRule (.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.adomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfold/ [NC]
RewriteRule (.*)$ /subfold/$1 [L]
# redirect for trailing slash
RewriteCond %{HTTP_HOST} www\.maindomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web/ [NC]
RewriteCond %{DOCUMENT_ROOT}/web%{REQUEST_URI} -d
RewriteRule (.*[^/])$ /$1/ [L,R=301]
RewriteCond %{HTTP_HOST} www\.maindomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web/ [NC]
RewriteRule (.*)$ /web/$1 [L]
I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html
In my hosting account, I have domains. One is located at the root and the other is in the /example/ folder. Here is the redirect code in my main .htaccess file, which works fine apart from the function I just described. This function was working previously, and has mysteriously stopped-
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^/example(.*)$ http://example.com/$1 [R=301,L]
# Rewrite /example to http://example.com
# Remove .php from file names and force added slash
# http://stackoverflow.com/questions/1068595/htaccess-code-to-remove-extension-and-addforce-trailing-slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
Any ideas?
Try removing the leading slash, like this:
RewriteRule ^example(.*)$ http://example.com/$1 [R=301,L]
I have the following HTAccess file:
ErrorDocument 404 /index.php?name=404
RewriteEngine on
# Add www. to any link without it
RewriteCond %{HTTP_HOST} ^mydomain\.co.uk$
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
# Set homepage
DirectoryIndex index.php
# Rewrite content pages
RewriteRule ^sub1/([A-Za-z0-9_-]*)/$ index.php?sub1=1&name=$1
RewriteRule ^sub2/([A-Za-z0-9_-]*)/$ index.php?sub2=1&name=$1
RewriteCond %{REQUEST_URI} !^/404/
RewriteRule ^([A-Za-z0-9_-]*)/$ index.php?name=$1
# Make the pages without .php work
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
This has gradually had bits and bobs tacked onto it and I now have a really small change to make, but can't work out how to do it.
The rules are basically there to ensure that all page have www and end in a /.
Most pages are in the form www.mydomain.co.uk/page/ which actually goes to mydomain.co.uk/index.php?name=page. There are two subdomains which pass an extra get param to the index.php.
The problem is that the homepage can be accessed from either www.mydomain.co.uk/ or www.mydomain.co.uk/index/.
How can I modify these rules so that if the user enters www.mydomain.co.uk it redirects to www.mydomain.co.uk/index/?
Try making the modification below to your .htaccess
### Existing Rules
# Add www. to any link without it
RewriteCond %{HTTP_HOST} ^mydomain\.co.uk$
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
##New Rules
#if request for www.mydomain.co.uk
RewriteCond %{HTTP_HOST} ^www\.mydomain\.co\.uk$
#and it is for the home page, then redirect to index
RewriteRule ^$ http://www.mydomain.co.uk/index/ [L,R=301]
##Other Existing rules go here