I am trying to configure and write my .htaccess file correctly. There are some things I already tried, but somehow they dont all work out.
I am trying to accomplish the following redirects:
Redirect from HTTP to HTTPS for all folders, including subfolders
Remove ".html" endings so the url shows www.domain.com/photos instead of www.domain.com/photos.html
Remove "index.html" when on home page, so it only shows www.domain.com
Some things work, but when going in a subdirectory like www.domain.com/sub, the page changes from HTTPS to HTTP.
Also, when accessing the page via HTTPS, it doesnt correctly load the font, that is specified in the HTML file
()
See my code below:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS. # i.e. http://www.example.com/foo/ to https://www.example.com/foo/ # The leading slash is made optional so that this will work either in httpd.conf or .htaccess context
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
I figured it out and it works so far:
<IfModule mod_rewrite.c>
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# Redirect to www
RewriteCond %{HTTP_HOST} !^www\.domain\.ch [NC,OR]
# Redirect to https
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://www.domain.ch%{REQUEST_URI} [NE,R=301,L]
# remove .html; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .html to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
Related
I'm trying to shorten my urls in the address bar and for Google indexation purposes. For example, a real server directory path
http://www.somewebsite.com/path1/path2/path3/
would display
http://www.somewebsite.com/path3/
I've found many similar topics but no answer that seems to work for this particular case.
I have for example:
RewriteRule ^path3(.*)$ path1/path2/path3$1 (tried with with [L], [QSA,l], [R=301,...]...)
But this simply does a redirect and does not keep the short address in the browser.
My .htaccess file looks as follow:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddCharset UTF-8 .html
# Force www.
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
#Trying to have a shorter url in address bar
RewriteRule ^path3(.*)$ path1/path2/path3$1
Converting my comments to answer so that solution can be easily found for the problem stated.
There are couple of issues with the rules shown in question:
Since target paths are pointing to an existing directory and you don't have trailing / in target it is causing an external redirect by mod_dir module of Apache that appends a / at the end of directory path and performs a 301 redirect.
Incorrect positioning of rule.
Not critical but missing L and NE (no escape) flag from redirect rules which may cause problems for some cases.
With those suggestion final working .htaccess can be like this:
# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddCharset UTF-8 .html
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Force www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# remove .php and index; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.php[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/$ $1 [R=301,L,NE]
# rewrite path3/ to /path1/path2/path3/
RewriteRule ^path3/.*$ path1/path2/$0 [L,NC]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*?)/?$ $1.php [L]
The example you provided works for myself on my own test website, using the below .htaccess rewrite:
RewriteEngine On
RewriteRule ^test2.txt/?$ /test.txt
This lets me have an optional trailing slash, and show the content of test.txt on /test2.txt.
Assuming your rewrite engine is on, can you replicate this behaviour? What version of Apache are you using? Is the path handled by a CMS at all?
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]
I have multiple domains that point to the same folder on my server. The code in index.php page recognizes which domain is accessing it and shows different content for each domain.
Now I want that www.domain-a.com\sitemap.xml opens /sitemap-a.xml so that each domain has its own sitemap.
I use the following rule in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
However, if I access www.domain-a.com\sitemap.xml then the content from the file /sitemap.xml is shown instead of the file sitemap-a.xml.
Is the redirecting rule wrong? If so, how can I fix it?
I am using Laravel. This is the full .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
# browser request: .php to non-php
# https://stackoverflow.com/questions/13222850/redirect-php-urls-to-urls-without-extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can you try this rule just above # Handle Authorization Header line:
RewriteCond %{HTTP_HOST} (?:^|\.)domain-a\.com$ [NC]
RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]
I'm trying to hide the .php file extension, it's working fine, but one page doesn't. All the files are in the root. When I try to visit the gastenboek (guestbook) I get a 404 error page. Some files of the guestbook are in a folder, like messages, send and captcha. But the main file of that page (Gastenboek.php) is in the root. There is another page, the forum, this is also in a folder, but that page is working just fine. How is it possible that the Gastenboek doesn't show up, and the forum does? What am I doing wrong? This is the code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^slotraceclub.nl$
RewriteRule (.*) http://www.slotraceclub.nl/$1 [R=301,L]
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
</IfModule>
I want to make all my URLs uniformly clean. Which means all my URLs have no extensions, nor trailing slash, and if a person does enter in a .php or trailing slash, it would simply redirect the person to the clean URL.
Example:
example.com/blog/file.php
and
example.com/blog/file/
would both redirect to
example.com/blog/file
In .htaccess try this:
RewriteEngine on
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
My web host recently (Sept 2011) updated their Apache servers to a newer version of Apache, and my old .htaccess code to display extensionless URLs no longer worked. After a considerable amount of copying code (that probably worked with older versions of the Apache OS) and unsuccessfully testing, I was able to determine that the following code seems to work on my server, and several others:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]
Give that a try - the -Multiviews seemed to be the key element to get it to work.
# Turn off the need of directory slash redirecting
DirectorySlash Off
# Turn on RewriteEngine
RewriteEngine On
# Internally redirect request to php file
RewriteCond "%{REQUEST_FILENAME}" !-f
RewriteCond "%{REQUEST_FILENAME}\.php" -f
RewriteRule "(.*)" "$1.php" [L]
# External redirects
# The above line is used to determine whether a internal redirect is done or not.
# If it is internally redirected, REDIRECT_STATUS will become 200.
# RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
# Redirect request with .php extension to extensionless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteRule "(.*)\.php$" "$1" [R=301]
# Redirect request with trailing slash to slashless
RewriteCond "%{ENV:REDIRECT_STATUS}" "^$"
RewriteCond "%{REQUSET_FILENAME} -d
RewriteCond "%{REQUSET_FILENAME} !-f
RewriteRule "(.*)\/" "$1" [R=301]