Messy HTAccess File Gives Duplicate Homepage - .htaccess

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

Related

How can I redirect a single segment url without forward slash at the end?

My website's htaccess file is not allowing a single segment's redirect without a forward slash at the end. If I leave it off, it loops and crashes the website. How can I allow my users to go to [site].com/winter-camping and have it be redirected? This is what I have set up that currently works:
RewriteEngine On
RedirectMatch permanent /find-a-park/ https://[site].com/about/find-a-park
RedirectMatch permanent /winter-camping/ https://[site].com/about/promotion-details/winter-camping
#our rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Basically I don't want my users to have to put the forward slash at the end to get to the page at all.
I do have instances that do work, however, which is why I'm confused. The only difference is they're subpages:
RedirectMatch permanent /parks/about/central-park https://[site].com/parks/central-park
One thing to know is that we use ExpressionEngine as our CMS. How can I get those first two redirects to redirect their urls and not crash my site? Thanks 🙃
You should use only mod_rewrite rule and use a regex with optional trailing slash:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-park/?$ https://[site].com/about/find-a-park [L,NC,R=301]
RewriteRule ^winter-camping/?$ https://[site].com/about/promotion-details/winter-camping [L,NC,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

How to write a subdomain with htaccess?

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]

Redirect /map/ to /file.php if /map/ does not exist using .htaccess

I'm trying to redirect users from www.hostname.com/map/ to www.hostname.com/map.php if /map/ does not exists. I have the following .htaccess file already:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Which does the following:
Redirect users automatically to the https:// variant of my website
Redirects /filename to /filename.php if there is no map called /filename/ without changing the visible url
But now I want to redirect users from /map/ to a file called /map.php as well if there is no map called /map/. Ofcourse I don't want to display the .php portion of the file name, so I basically want users to be redirected to /filename if the map /filename/ does not exists without changing the visible url. How can I rewrite the code above to make this possible?
You can use:
RewriteEngine On
RewriteBase /
# keep redirect rule before internal routing one
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# if matching .php file exists forward it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

.htaccess 301 Redirect non slash domains and slash domain to .html

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

.htaccess, URL changes but loading content

I'm having an issue with .htaccess where I have managed to successfully rewrite the URLs but the content is no longer loading.
The following is my htaccess file.
I'm aiming for all my .html pages (the site is made of .html static pages) to have their extensions removed. However, I require the .html URLs to 301 redirect to the new URL's so that my SEO does not take a hit from these changes.
Example:
Original: www.example.co.uk/page.html
Desired: www.example.co.uk/page/
It is important that the original URL redirects the new URL's though.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(([^/]*/)*[^/.]+)$ $1.html [L]
I have tried the htaccess above and I've also tried the variation below, but neither has worked as desired. Any help would be much appreciated.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
You need to change 2 things.
Add this right after your redirect to prevent a redirect loop:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
change the last rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html [L]

Resources