Setting up prefered versions in .htaccess - .htaccess

I'm trying to set up an .htaccess file. I wanted to set it to go to just the route of the current folder if the user include index.php and index.html at the end of the address.
I was want the www to be added to the address if the user missed it out.
I'm running two websites from the same address. One is using the root folder and the other from a folder. The index bit works fine for both but when I set the www, if they miss it out, it goes to the home page on the root rather than the folder.
Could anyone suggest what I need to do here and where I should put the .htaccess file to get it to work?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^candle-light\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.candle-light.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.candle-light.co.uk/ [R=301,L]
</IfModule>
The subfolder is called flickering.

Try these rules with %{REQUEST_URI} variable instead of $1:
RewriteEngine On
# skip POST requests
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
# add www
RewriteCond %{HTTP_HOST} ^candle-light\.co\.uk$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.(php|html) [NC]
RewriteRule ^(.*)index\.(?:html|php)$ /$1 [L,R=301,NC,NE]

Related

Redirect 404 error with public_html subfolder as a root folder for website

I used to run a Wordpress website, but started building my own in php. I was building it on a subdirectory on my server and now want to launch the new website. The structure is as follows:
public_html/newversion/index.php
public_html/newversion/category.php
public_html/newversion/category/information.php
I've edited the .htaccess file in public_html to force non-www and https, set newversion as the new document root, and some code to remove the .php extension from URLs (see the .htaccess code below).
That means that I want newversion/index.php to show up when someone goes to example.com, instead of the person needing to go to example.com/newversion/index.php.
Everything works fine, except when I try to access category.php or information.php. Then the browser is automatically redirected to example.com/newversion/category.php and I get a 404 error. When I try to access information.php, the browser tries to open example.com/category/information.php, but I also get a 404 error.
What am I doing wrong?
(FYI, Wordpress is not installed anymore)
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Base Redirects #
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://example.com/$1/ [R=301,L]
# Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# Change root folder to subfolder of public_html
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/newversion%{REQUEST_URI} -l
RewriteRule ^ /newversion%{REQUEST_URI} [L]
RewriteEngine On
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
# Set php version
AddHandler application/x-httpd-recommended-php .php .php5 .php4 .php3
</IfModule>
# Restrict access
<Files .htaccess>
order allow,deny
deny from all
</Files>
Took me a long time figuring this out, but I now know what people mean with .htaccess Rewrite voodoo... So, for anyone still needing an answer:
In the end, the order in which you declare htaccess rules and the subdirectory in which you do it matters a lot. Don't ask me how it works exactly, because I don't fully understand. However, this code worked for me.
The underlying code achieves the following:
website root in subdirectory
force non-www and https
remove .php extension from url
open subdirectories as example.com/foo/ whether or not entered with trailing slash
open files in subdirectories as example.com/foo/bar whether or not entered with a trailing slash
.htaccess file in public_html:
# htaccess for public_html
Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php
# Remove www and force https
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/subdirectory/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301]
#Prevent direct access to PHP Scripts
RewriteCond %{THE_REQUEST} \.php[?/\s] [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^/?$ subdirectory/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!subdirectory/).*)$ subdirectory/$1 [L,NC]
Then in the subdirectory add the following htaccess file:
# htaccess for subdirectory
# Remove trailing slashes (keep as first rule!)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
# Open page without php extension
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

301 redirect not working in .htaccess

I have two domains, autodromodifranciacorta.it and franciacortacircuit.com both pointing to the same website hosted on this IP address: 94.23.64.40.
Now i want all the content to be under one single domain, so i decided to 301 redirect all the traffic from franciacortacircuit.com to autodromodifranciacorta.it
Here is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The redirect is not working and i have no clue why, because the syntax looks correct to me.
The .htaccess file is processed, because if i put a typo in it, i get server error.
Wha'ts wrong with it?
Your second http_host condition is wrong and it never matches if the current host is franciacortacircuit.com, You need to use an OR condition to match against 2 diffrent hosts.
#Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]

.htaccess 301 redirect subdomain and all files and folders to root

I have been beating my head up against a wall for a day trying to figure this out. If you know how to do this and can help, it would be greatly appreciated.
I am trying to 301 redirect everything from www.example.com to the root domain at example.com
The following works to redirect www.example.com as intended, but requests for other files or folders are returning a 302. This is a Wordpress Multisite install, but it only has one domain mapped at this time.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# BEGIN WordPress
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
# END WordPress
# Link re-directs from old website.
RewriteCond %{HTTP_HOST} ^www.example.com/(.*)$ [NC]
RewriteRule ^/?(.*)$ http://example.com$1 [R=301,L]
# Other
php_value memory_limit 500M
Can anyone help?? Please and thank you.
YOu need to add your redirect rule before all of your routing rules (e.g. wordpress stuff). You are also missing a slash in your target and the HTTP_HOST does not contain any path info, just the hostname. So this:
# Link re-directs from old website.
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^/?(.*)$ http://example.com/$1 [R=301,L]
needs to go right under the RewriteEngine On at the very top of the htaccess file.

htaccess redirect not working as thought

Ok so i have an admin folder in a site that i am working and what i need is when someone enters
http://admin.teamfocususa.org/
in their browser then they get redirected to the admin folder
here is my htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^teamfocususa.org [NC]
RewriteRule ^(.*)$ http://www.teamfocususa.org$1 [L,R=301]
RedirectMatch 301 ^/admin/(.*)$ http://admin.teamfocususa.org/$1
Redirect http://admin.teamfocususa.org /admin
this part is not working
Redirect http://admin.teamfocususa.org /admin
any ideas on how to make this part work ...when i visit http://admin.teamfocususa.org is the home page and not the admin folder as i thought....FYI this is a shared host and I dont have access to the vhost to fix this the way i know
Try adding a RewriteRule matching on requests to / of admin.teamfocususa.org.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^teamfocususa\.org^ [NC]
RewriteRule ^(.*)$ http://www.teamfocususa.org$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?teamfocususa\.org [NC]
RewriteRule ^admin(.*) http://admin.teamfocususa.org/$1 [L,R=301]
# Not needed
#RedirectMatch 301 ^/admin/(.*)$ http://admin.teamfocususa.org/$1
# Added = requests to http://admin.teamfocususa.org/
# Redirected into http://admin.teamfocususa.org/admin
# --OOPS fixed RewriteCond - was typo RewriteRule
RewriteCond %{HTTP_HOST} ^admin\.teamfocususa\.org$ [NC]
RewriteRule ^$ admin/ [L,R=301]
# To hide /admin on admin.teamfocususa.org
# use this instead of the above group...
RewriteCond %{HTTP_HOST} ^admin\.teamfocususa\.org$ [NC]
RewriteRule ^$ admin/ [L]
# Not needed
#Redirect http://admin.teamfocususa.org /admin
I do not think you want to do a Redirect, but a simple RewriteRule to show the admin folder for the domain admin.teamfocususa.org
RewriteEngine On
RewriteBase /
# If not admin.teamfocususa.org and in /admin folder, redirect to admin.teamfocususa.or
RewriteCond %{HTTP_HOST} ^admin.teamfocususa.org [NC]
RewriteRule ^/admin/(.*)$ http://admin.teamfocususa.org/$1 [L,R=301]
# If domain is admin.teamfocususa.org, show files from admin folder
RewriteCond %{HTTP_HOST} admin.teamfocususa.org [NC]
RewriteRule (.*)$ /admin$1 [L]
# if domain is teamfocususa.org, redirect to www.teamfocususa.org
RewriteCond %{HTTP_HOST} ^teamfocususa.org [NC]
RewriteRule ^(.*)$ http://www.teamfocususa.org$1 [L,R=301]
UPDATE: My domain checks were wrong, try this updated version

how to modify .htaccess file to always redirect to www

I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.
Here is my current htaccess file:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Normally I know how to do the redirect, but im having issues since it already has those few lines in there.
I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
(edited to have all the code in the same block)
Add something like this immediately after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
There are two methods available
1) use mod_alias apache module
Redirect permanent /something http://yourwebsite.com/something
2) Add the following entry in your .htaccess / http.conf / yourwebsite.conf in the webserver configuarion directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]
If you want redirect example.com to www.example.com you can try below code
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

Resources