Can't redirect using 301 and .htacces - html

I've just begun learning CSS and HTML.
I bought a domain from HostGator to upload my tiny website.
I used CyberDuck to upload all of my things.
Then I found out about a thing called .htacces which I did not have in the WWW folder and I've tried "making" one.
Then I tested the site using Nibbler and it keeps saying "There is duplicate content" and that I should do this: "Choose one domain that you would like to use and then set a permanent (HTTP 301) redirect to forward users who visit the other."
I found a lot of REWRITES AND REDIRECTS but I could not to make any of them work.
What do I need to write to not have a "duplicate" site (or to solve the Canonical Website Address (This is how it's called I think)) ???
Let's say my site name is: http://www.FirstSite.net and I want all "FirstSite" searches to go to https://FirstSite.net or FirstSite.net but still being a https.
I wrote https with an s because I want to make the site secure (if that is even possible).
I've been modifying the .htacces from gator's cPanel and I am using Google Chrome.
Thank you for reading this manuscript !
This is all that I've copy-paste into it (I forgot what those without a # do (the last 2 paragraphs)) :
# MAIN DEFAULTS
Options +ExecCGI -Indexes
DirectoryIndex index.html
DefaultLanguage en-US
AddDefaultCharset UTF-8
ServerSignature Off
# compress text, HTML, JavaScript, CSS, and XML
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
# remove browser bugs
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
#HTACCES Security
<Files ~ “^.*\.([Hh][Tt][Aa])”>
order allow,deny
deny from all
satisfy all
</Files>
# 1 WEEK 1 DAY
<filesMatch “.(jpg|jpeg|png|gif|swf|ico)$”>
Header set Cache-Control “max-age=691200, public”
</filesMatch>
# 1 WEEK 1 DAY
<filesMatch “.(xml|txt|js)$”>
Header set Cache-Control “max-age=691200, proxy-revalidate”
</filesMatch>
# 1 WEEK 1 DAY
<filesMatch “.(html|htm|css|php)$”>
Header set Cache-Control “max-age=691200, private, proxy-revalidate”
</filesMatch>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>

To make your website secure you must have a valid SSL certificate. It's not just putting s with http
If you already have one installed with your domain. You can use the code below for you domain address to force SSL(redirect).
RewriteCond %{HTTP_HOST} firstsite\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.firstsite.net/$1 [R,L]
To resolve the canonical or duplicate website issue, please use
# Fix the non-www to www canonicalization issue:
RewriteCond %{HTTP_HOST}^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# Fix the canonical issue vecause of index files
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . http://www.firstsite.net%1/%2 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([^/]+/)*)index\.html\ HTTP/
RewriteRule index\.html$ http://www.firstsite.net/%1 [R=301,L]\
Change firstsite.net with your domain name and the name and extension of the index file based on your server settings.
If Using ngnix Server Redirect www to non-www
Strip www from url with nginx redirect
server {
listen 80;
server_name www.example.com;
return 301 http://example.org$request_uri;
}
server {
listen 80;
server_name example.com;
...
#The rest of your configuration goes here#
}
So you need to have TWO server codes.

Related

.htaccess mobile redirect seen by all visitors after first mobile hit

I very recently migrated to a new web host.
I have a redirect in htaccess, that sends mobile users to a mobile site, based on the detected user_agent. This htaccess and associated website/s worked perfectly with my old host.
With my current host, whenever I enable this rewrite rule, as soon as a mobile user visits the root of desktop site, they as expected, get redirected to the mobile site however, all subsequent requests to the desktop site on any device, get redirected to the mobile site. i.e. it appears that the redirect to the mobile is cached and that cached redirect then gets served to all visitors regardless of user_agent until I disable the htaccess and they clear their caches.
My hosting company say that they are not developers and they cannot help with my code, that there is something wrong with it that's causing this issue but they can't tell me what. :-|
Please could someone cast their eyes over the below and tell me what if anything, is wrong with it.
ps. This is shared hosting so i don't have access to the logs.
RewriteEngine on
# http://mywebsite.co.uk => https://www.mywebsite.co.uk
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
# https://mywebsite.co.uk => https://www.mywebsite.co.uk
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
## Redirect to mobile site
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|mobile.+firefox|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ ce|xda|xiino [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-) [NC]
RewriteCond %{HTTP_HOST} !^mobile\. [NC]
RewriteRule ^(.*)$ https://mobile.mywebsite.co.uk [E=NO_CACHE,R,L]
Header always set Cache-Control "private, no-cache" env=NO_CACHE
Adding my entire htaccess here if that helps?
# Use HTTP Strict Transport Security to force client to use secure connections only
<IfModule mod_headers.c>
Header set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" env=HTTPS
<FilesMatch "\.(js|css|xml|gz)$">
</FilesMatch>
Header set X-XSS-Protection: "1; mode=block"
Header set Cache-Control "no-store, max-age=0"
</IfModule>
Header always append X-Frame-Options SAMEORIGIN
Header set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy no-referrer
Header append Vary: Accept-Encoding
RewriteEngine on
# http://mywebsite.co.uk => https://www.mywebsite.co.uk
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
# https://mywebsite.co.uk => https://www.mywebsite.co.uk
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
<IfModule mod_rewrite.c>
## Redirect to mobile site
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge\ |maemo|midp|mmp|mobile.+firefox|netfront|opera\ m(ob|in)i|palm(\ os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows\ ce|xda|xiino [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a\ wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r\ |s\ )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1\ u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp(\ i|ip)|hs\-c|ht(c(\-|\ |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac(\ |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt(\ |\/)|klon|kpt\ |kwc\-|kyo(c|k)|le(no|xi)|lg(\ g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-|\ |o|v)|zz)|mt(50|p1|v\ )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v\ )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-|\ )|webc|whit|wi(g\ |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-) [NC]
RewriteCond %{HTTP_HOST} !^mobile\. [NC]
RewriteRule ^(.*)$ https://mobile.mywebsite.co.uk [E=NO_CACHE,R,L]
Header always set Cache-Control "private, no-cache" env=NO_CACHE
</IfModule>
## 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>
If you are making redirect decisions based on the user agent, you need to tell caching proxy servers and CDNs that they need to check to see how each user agent redirects rather than returning the same result as the first user.
To do that in .htaccess you can just add the line:
Header append Vary User-Agent

Can anyone access protected (with .htaccess) json files in the host?

I have important config.json file, contains important data (like database username, password, etc...) and I'm protecting it using htaccess.
Can anyone access this file using any method (like hacking or anything like that)?
Full htaccess code:
<IfModule mod_rewrite.c>
DirectoryIndex index.php index.html
RewriteEngine On
# RewriteBase /
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
Options -Indexes
# protect files
<FilesMatch "\.(sql|data|conf|dat|json|inf|info)$">
Order allow,deny
Deny from all
</FilesMatch>
</IfModule>
** Related Info: The server I'm using is apache with PHP code. Also my files is in a shared hosting company. **

How to write an exception for a specific folder

I`ve been tasked to modify the redirect to a mobile site in .htaccess file to redirect only the main page. The problem is that the main folder includes many subfolders that are pages of their own and they wrongly redirect to the main site mobile version.
This is the code:
RewriteOptions inherit
<IfModule mod_expires.c>
ExpiresActive On
#ExpiresDefault "access plus 10 second"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
# Webfonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/eot "access plus 1 year"
ExpiresByType font/opentype "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/font-woff "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType application/font-woff2 "access plus 1 year"
</IfModule>
<filesMatch ".(js)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<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>
#### MOBILE REDIRECTION START
RewriteBase /
# Check if m=1 is set and set cookie 'mobile' equal to 1
RewriteCond %{QUERY_STRING} (^|&)m=1(&|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [CO=mobile:1:%{HTTP_HOST}]
# Check if m=0 is set and set cookie 'mobile' equal to 0
RewriteCond %{QUERY_STRING} (^|&)m=0(&|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [CO=mobile:0:%{HTTP_HOST}]
# cookie can't be set and read in the same request so check
RewriteCond %{QUERY_STRING} (^|&)mobile=0(&|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ - [S=1]
# Check if this looks like a mobile device
# Check if we're not already on the mobile site
# Check to make sure we haven't set the cookie before
# Now redirect to the mobile site
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC,OR]
RewriteCond %{HTTP:Profile} !^$
RewriteCond %{HTTP_HOST} !^m\.
RewriteCond %{HTTP:Cookie} !\mobile=0(;|$)
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ http://m.siteurl.com [R,L]
#### MOBILE REDIRECTION END
Can someone help me where and how to put the exceptions for folders(sites) other than the main one.
RewriteRule ^ http://m.siteurl.com [R,L]
The very last RewriteRule directive redirects everything (that matches the regex ^) to the root of the mobile site (although you are strictly missing the trailing slash on the target URL, which the browser will need to append for you.)
To "redirect only the main page" you need to be more specific with the regex and match an empty URL-path only. eg. ^$. For example:
RewriteRule ^$ http://m.siteurl.com/ [R,L]
However, the user could presumably enter the site at any page, so do you not want to redirect any URL to the corresponding URL-path path on the mobile site?
For example, to redirect /<anything> to http://m.siteurl.com/<anything> you need to capture the URL-path using the RewriteRule pattern. For example:
RewriteRule (.*) http://m.siteurl.com/$1 [R,L]
Where $1 is a backreference to the captured URL-path (ie. (.*)).
How to write an exception for a specific folder
The title of your question seems to conflict with what you are asking in the main body of the question, ie. redirecting only the main page.
However, to answer this... to make an "exception for a specific folder", you would add an additional RewriteCond directive preceding the above RewriteRule directive to check that the request does not map to the "specific folder".
For example:
RewriteCond %{REQUEST_URI} !^/specific-folder/
RewriteRule (.*) http://m.siteurl.com/$1 [R,L]
Only if the requested URL does not (! prefix) start with /specific-folder/ will the following RewriteRule substitution occur.
Aside:
Is your mobile site not using HTTPS?
RewriteOptions inherit - What directives are you "inheriting"?
Thanks for the answer. I will clarify a little bit more.
I created a new site on the hosting that has a number of other (mucher older) sites. As I tried to enter the site from my mobile phone I realized that it is redirected to the mobile site of that "main site".
I took a look at public_html folder and realized that someone created a site a long time ago in public_html folder with a redirect in .htaccess and then someone else created a number of subfolders with other sites, including a subfolder for the site that I created and uploaded to their server.
The problem now is that those sites in the subfolders get redirected to the mobile site of the site that is created in the main public_html folder.
To answer the "aside" part - It is not my site and I believe that it doesnt. It is a very old site that needs a complete rehaul. Rewrite options inherit - thats my question too. Im not sure, as Im not the creator. It`s complete mess, but for now I just need to resolve the redirect problem.
I hope that clarifies it a bit.
EDIT:
It seems that removing Rewrite options inherit did the trick. Only the main site gets redirected now to the mobile version.

Why the lack of the trailing slash in url adds :8080 to redirect?

I have two website on an Ubuntu/Apache server.
One is in directory
/var/www/html/dir1
and is configured as virtual server in apache with FollowSymlink option activated. Replies to
http://example.com
The second is a webpage index.html in
/var/www/html/dir2
To obtain a simple way to link the second page to url
http://example.com/dir2
I made a soft link inside /var/www/html/dir1 to /var/www/html/dir2 named dir1 and all went ok.
Visiting
http://example.com/dir2/
lead to the page index.html in /var/www/html/dir2.
So far so good.
Though, visiting
http://example.com/dir2
(note the lack of the trailing slash)
cause the redirect to
http://example.com:8080/dir2/
Why in this case the port gets added? How can I easily remove it?
EDIT: I need to remove the port because from index.html some post request is made and the :8080 part is causing me some cors problems.
EDIT: Virtual Host conf
<VirtualHost *:8080>
DocumentRoot "/var/www/dir1/drupal"
ErrorLog /var/log/apache2/dir1-error.log
CustomLog /var/log/apache2/dir1-access.log combined
<Directory "/var/www/dir1/drupal/">
Options ExecCGI FollowSymLinks
AllowOverride all
Allow from all
Order allow,deny
</Directory>
</VirtualHost>
htaccess in dir1
#
# Apache/PHP/Drupal settings:
#
# Protect files and directories from prying eyes.
<FilesMatch "/fb/|/phpMyAdmin/|\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$">
Order allow,deny
</FilesMatch>
# Don't show directory listings for URLs which map to a directory.
Options -Indexes
# Follow symbolic links in this directory.
Options +FollowSymLinks
# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php index.html index.htm
# Override PHP settings that cannot be changed at runtime. See
# sites/default/default.settings.php and drupal_initialize_variables() in
# includes/bootstrap.inc for settings that can be changed at runtime.
# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
php_flag magic_quotes_gpc off
php_flag magic_quotes_sybase off
php_flag register_globals off
php_flag session.auto_start off
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_flag mbstring.encoding_translation off
php_value upload_max_filesize 100M
php_value post_max_size 100M
</IfModule>
# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
# Enable expirations.
ExpiresActive On
# Cache all files for 2 weeks after access (A).
ExpiresDefault A1209600
<FilesMatch \.php$>
ExpiresActive Off
</FilesMatch>
</IfModule>
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule "(^|/)\." - [F]
RewriteRule ^js\/(.*)$ /sites/all/modules/ets/jslib/$1 [L]
RewriteRule (.*)jslib\/\d+\/(.*)$ $1jslib/$2 [L,QSA]
RewriteRule (.*)experiments\/\d+\/(.*)$ $1experiments/$2 [L,QSA]
RewriteRule (.*)exp_packages\/\d+\/(.*)$ $1exp_packages/$2 [L,QSA]
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
# Rules to correctly serve gzip compressed CSS and JS files.
# Requires both mod_rewrite and mod_headers to be enabled.
<IfModule mod_headers.c>
# Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
<FilesMatch "(\.js\.gz|\.css\.gz)$">
# Serve correct encoding type.
Header set Content-Encoding gzip
# Force proxies to cache gzipped & non-gzipped css/js files separately.
Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>
</IfModule>
Add this rule just below RewriteEngine On line:
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule [^/]$ %{REQUEST_URI}/ [L,NE,R=301]
and retest in a new browser or completely clear browser cache.

.htaccess this web page has a redirect loop htaccess

I want to redirect all my http traffics to https in my website.
I have following codes in my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
As soon as I place this file in a directory, I get an error saying "This web page has a redirect loop htaccess"
I cannot see anyother file that is causing any redirection.
What should I do?
This redirects all traffic on port 80 (standard http) to https:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Or, the opposite- redirect any traffic NOT on port 443 (https):
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
You can also try:
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]