I have one issue with my htaccess redirection code.
Suppose I request this url
https://example.com/about-us/
Then its redirect to non-slash link that means they redirect to
https://example.com/about-us
but what happens in between
https://example.com/about-us/ this url redirect to
https://example.com/about-us.html then redirect to
https://example.com/about-us
I want to remove this middle step from redirection.
I attached my htaccess code for better understanding
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteCond %{HTTP_USER_AGENT} !Twitterbot [NC]
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
#RewriteCond %{HTTP_USER_AGENT} !Twitterbot [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} "blog"
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} "blog"
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.html -f
# remove the .html extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
#Alternate default index page
DirectoryIndex index.html
## 404 Page
ErrorDocument 404 https://example.com/404
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
#ExpiresDefault "access plus 2 days"
</IfModule>
## EXPIRES CACHING ##
# END WordPress
Thank you in advance
Keep all your redirect rules at top:
Options -MultiViews
RewriteEngine On
RewriteBase /
## remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_USER_AGENT} !Twitterbot [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
# remove the .html extension
RewriteCond %{THE_REQUEST} ^GET\ (\S+)\.html\ HTTP [NC]
RewriteRule ^ %1 [R=301,L,NE]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}.html [L]
# remaining rules go below this
Make sure to use a new browser for your testing or completely clear your browser cache.
About Options -MultiViews: Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
Related
I have tried many things but failed. Something work for http to https and some for www to non www. But when I try modification for both http to https along with www to non www it broke my website.
Please help me in .htaccess modification for both rules
This the full of default .htaccess file
It successfully redirects http to https and www to non-www. but when I type http://www.xzy.com page show blank
# Case 1
# -------------------------------------------------------#
# RewriteEngine On
# DirectoryIndex index.html index.php
# RewriteCond %{HTTPS} !=on
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ index.php/$1 [L]
# -------------------------------------------------------#
# Case 2
# -------------------------------------------------------#
# RewriteEngine on
# ExpiresActive On
# ExpiresDefault A2592000
# RewriteCond %{SERVER_PORT} 80
# RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# RewriteCond $1 !^(index\.php|images|public|assets|uploads|themes|install|backups|updates|asset|mob|robots\.txt|curl.php)
# RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
# -------------------------------------------------------#
# Case 3
# -------------------------------------------------------#
# <IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteCond %{REQUEST_URI} ^system.*
# RewriteRule ^(.*)$ index.php?/$1 [L]
# RewriteCond %{REQUEST_URI} ^application.*
# RewriteRule ^(.*)$ index.php?/$1 [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ index.php?/$1 [L]
# </IfModule>
# -------------------------------------------------------#
# Case 4
# -------------------------------------------------------#
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# -------------------------------------------------------#
# Case 6 for SSL Hosting
# -------------------------------------------------------#
# RewriteEngine on
# ExpiresActive On
# ExpiresDefault A2592000
# RewriteCond %{SERVER_PORT} 80
# RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# RewriteCond $1 !^(index\.php|images|public|assets|uploads|themes|install|updates|asset|mob|robots\.txt)
# RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
# -------------------------------------------------------#
# case 5
# -------------------------------------------------------#
#RewriteEngine On
#DirectoryIndex index.html index.php
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule (.*) index.php/$1
# -------------------------------------------------------#
# secure GIT files
RedirectMatch 404 /\.git
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
It successfully redirects http to https and www to non-www. but when I type http://www.xzy.com page show blank
Try this code to solve your issue
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php/?$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
I read many similar questions but I did not find a solution. I want to redirect HTTP to HTTPS on the home page only.
http://example.com to https://example.com
I don't know anything about .htacces.
Here you can see how it looks now, it contains code to hide .php extensions, expires caching and redirect www to non-www. I found these examples online:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
#AddHandler application/x-httpd-php54 .php54 .php
AddHandler application/x-httpd-php70 .php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
To redirect only the homepage to HTTPS (and canonicalise the www subdomain) then add the following at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^$ https://%1/ [R=302,L]
Note that this is a 302 (temporary) redirect. Only change it to a 301 (permanent) when you are sure it's working OK to avoid browser caching issues.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Your existing www to non-www redirect at the end of the file also needs to be modified, otherwise it will redirect the homepage back to HTTP. This should also be moved to the top of your .htaccess file, immediately after the HTTP to HTTPS redirect.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.+)$ http://%1/$1 [R=301,L]
Note that I changed ^(.*)$ to ^(.+)$ to match 1 or more characters - thus avoiding the homepage. You can also simplify this to (.+) (ie. remove the anchors) if you wish. Regex is greedy by default, the anchors are superfluous.
Summary
RewriteEngine On
# Only redirect homepage to HTTPS (and remove www subdomain)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^$ https://%1/ [R=302,L]
# Remove www subdomain on other pages (HTTP only)
# >>> This is moved from the end of the htaccess file
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule (.+) http://%1/$1 [R=302,L]
# Remainder of htaccess file....
:
:
Clear your browser cache and change 302 to 301 only once you have confirmed it's working OK.
Aside: You can remove the RewriteEngine directive that appears later in the file if you wish. This is optional, it only needs to occur once. Being at the top, before the mod_rewrite directives, is logical (but not strictly required).
i have been contiously upgrading my htaccess file. however, there are 2 issues I just cant seem to fix. I have tried several approaches but nothing really worked for me.
my website: https://www.zeroohm.com/
my questions:
How can I remove the extra trailing slash when i go to zeroohm.com
currently, if i go to zeroohm.com, it redirects to https://www.zeroohm.com//
if i go to www.zeroohm.com/robots.txt/ , it stops me from viewing my robots.txt file (an error occured while processing this directive). how can i fix this? this problem happens to sitemap.xml and it is causing problems with webmaster tool too.
C. any ideas on improving my re-directs?
my htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} ^zeroohm\.ae$ [OR]
RewriteCond %{HTTP_HOST} ^www\.zeroohm\.ae$
RewriteRule ^/?$ "http\:\/\/www\.zeroohm\.com\/\$1\/" [R=301,L]
# check if url has www or not, add www if not available. if avaiable dont do anything.
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*) https://www.%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
#end
# force incoming traffic coming from HTTP to Https
RewriteCond %{HTTPS} !on$
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#end
#force trainling slash
RewriteBase /
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
#end of force trailing slash
# 1.To use URL Alias you need to be running apache with mod_rewrite enabled.
# 2. In your opencart directory rename htaccess.txt to .htaccess.
# For any support issues please visit: http://www.opencart.com
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymlinks
Options +SymLinksIfOwnerMatch
# Prevent Directoy listing
Options -Indexes
# Prevent Direct Access to files
<FilesMatch "\.(tpl|ini|log)">
Order deny,allow
Deny from all
</FilesMatch>
# cache to keep it at client pc
<ifmodule mod_expires.c>
Expiresactive on
# set default
Expiresdefault "access plus 24 hours"
Expiresbytype image/jpg "access plus 1 months"
Expiresbytype image/gif "access plus 1 months"
Expiresbytype image/jpeg "access plus 1 months"
Expiresbytype image/png "access plus 1 months"
Expiresbytype text/css "access plus 1 months"
Expiresbytype text/javascript "access plus 1 months"
Expiresbytype application/javascript "access plus 1 months"
Expiresbytype application/x-shockwave-flash "access plus 1 months"
</ifmodule>
#gzip compression
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
# SEO URL Settings
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
#libwww-perl security fix
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{THE_REQUEST} \ /index\.php\?_route_=?([^&\ ]*)
RewriteRule ^ /%1? [R=301,L]
You can keep these rules in your site root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?zeroohm\.ae$ [NC]
RewriteRule ^ http://www.zeroohm.com%{REQUEST_URI} [R=301,L,NE]
# check if url has www or not, add www if not available. if avaiable don't do anything.
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
#libwww-perl security fix
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule ^ - [F,L]
RewriteCond %{THE_REQUEST} \ /index\.php\?_route_=?([^&\s]*)
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^sitemap\.xml$ index.php?route=feed/google_sitemap [L,QSA,NC]
RewriteRule ^googlebase\.xml$ index.php?route=feed/google_base [L,QSA,NC]
RewriteRule ^download/ /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule (.*) index.php?_route_=$0 [L,QSA]
Clear your browser cache before testing this change.
In the code above are the lines that i have in my .htaccess file. The problem is when is a single command works like a charm but when i add many commands doesnt work any of thems. Please can anyone help me to find the error.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www.marinoswood.gr$ [NC]
RewriteRule ^(.*)$ http://www.marinoswood.gr/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^185\.4\.133\.44
RewriteRule (.*) http://www.marinoswood.gr/$1 [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
A few rules of thumb:
Always put your redirect rules before any internal rewrite rules, so anything with an R flag must be first
If you are using the F flag, in a rule to block access, those rules must be before all rules, including your redirect rules (ones with R flag)
Rewrite conditions only apply to the immediately following rule, so if the conditions need to be applied to multiple rules, those conditions must be repeated for each of them
Those .php rules are completely broken and need to be rewritten
Additionally, the mod_expires directives do not impact the rules.
So maybe you want something like?:
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{HTTP_HOST} !^www.marinoswood.gr$ [NC]
RewriteRule ^(.*)$ http://www.marinoswood.gr/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^185\.4\.133\.44
RewriteRule (.*) http://www.marinoswood.gr/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
</IfModule>
I have been trying to get FURLs working in MODx. Following resource covers this topic in details - http://rtfm.modx.com/display/revolution20/Using+Friendly+URLs. However once the changes to .htaccess are made, the site is no longer available. Here is my .htaccess file:
# Friendly URLs Part
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} .
# Force all pages to go to www.domain.com for SEO
RewriteCond %{HTTP_HOST} !^www\.dev\.domain\.co\.uk [NC]
RewriteRule (.*) http://www.dev.domain.co.uk/$1 [R=301,L]
# Friendly URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
# Additional Settings Follow
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/png A2592000
BrowserMatch "MSIE" brokenvary=1
BrowserMatch "Mozilla/4.[0-9]{2}" brokenvary=1
BrowserMatch "Opera" !brokenvary
SetEnvIf brokenvary 1 force-no-vary
Any ideas what may be the issue?
Change .htaccess back to original - https://github.com/modxcms/revolution/blob/develop/ht.access
Try this Default htaccess file for modx
RewriteEngine On
RewriteBase /
#RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Rewrite secure requests properly to prevent SSL cert warnings, e.g. prevent
# https://www.example.com when your cert only allows https://secure.example.com
#RewriteCond %{SERVER_PORT} !^443
#RewriteRule (.*) https://example.com/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Check this for full htaccess file