https rules for htaccess including www redirect and removal of ".html" extensions - apache

Currently my .htaccess looks like this and works perfect for http.
It redirects to www. and removes the .html file extension.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I have tried the .htaccess from this answer but still the site in question is completely messed up. E.g https://example.com/work shows a 404.
Also all images that are linked in the source code with /img/example-01.jpg do not show.
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]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can you please help me with getting a .htaccess file that:
1.
Redirects to https
2.
Redirects to www. subdomain
3.
Removes .html from file extension so that example.com/work shows the work.html page.
Thank you for your help.

If your current .htaccess works fine, this modification should do:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{REQUEST_SCHEME} !https
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Related

Need to redirect 301 a URL in .htaccess file but it adds extra http//?

I am trying to redirect /abc.html to /abc.php but when I did it gives an extra http// and page is not working like http//www.example.de/abc.php don't know from where this HTTP comes.
note: website is not with ssl so domain name is http://example.de
My .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://example.de/$1 [R=301,L]
RedirectPermanent /tour.html /tour.php
With your shown samples/attempts, could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
##To serve home page link.
RewriteRule ^/?$ index.php [L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^([^.]*)\.html/?$ $1.php [NC,L]

Too many redirects with mod_rewrite (at most one redirect?)

I want there to be at most one redirect using my htaccess file. Is that possible?
my htaccess file:
RewriteEngine On
#remove index/index.html
RewriteRule index\.html|index https://example.com/ [R=301,L]
#redirect to non-wwww and https site
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]
#remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
If I enter for example http://example.com/foo.html or http://example.com/index then I have two redirects.
If I enter for example http://example.com/ or https://example.com/foo.html then I have only one redirect.
What I want:
I want at most one redirect (see last two examples).
If I navigate to the home page, then I always want the index/index.html to be removed
All http calls should be redirected to https calls
All URL which are called with www, should be called without www
If I navigate to the subpage (https://example.com/foo.html), then only the html should be removed respectively the user can call the page without .html (https://example.com/foo).
Is this possible? Currently sometimes two redirections happen.
Could you please try following, based on your shown samples. Please make sure you clear your browser cache before testing URLs.
RewriteEngine ON
##Apply https and remove www together for all requests here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [NE,L]
##Server files with backend .html files.
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ $1.html [L]
##Deal with home page .html stuff here.
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ index/index.html [L]

Keep path when rewriting with .htaccess

I recently moved my webpage from HTTP to HTTPS and I now got a couple of things that doesn't work like before.
The problem I'm having is that when a user is trying to go to:
http://example.com/i/<imageID> they are redirected to:https://www.example.com/i/image.php?id=<imageID>. This is "correct", but not what I want. I want the links to stay at /i/<imageID> regardless of the user came from http or https.
I have two .htaccess-files that I use to control this redirection on my webpage.
First .htaccess (root directory):
RewriteEngine on
RewriteOptions inherit
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Second .htaccess (image directory - /i):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^=]+)$ image.php?img=$1 [NC,L]
This redirection was working just fine before I added the first .htaccess-file that redirects the user from HTTP to HTTPS, but now it seems to be broken.
I'm not that familiar with htaccess-files so I hope someone out there can help so I can get my clean URLs back. :)
UPDATED ANSWER --> updated again
%{REQUEST_URI} only includes the URI WITHOUT the querystring. But this is stored in and can be retrieved in another variable --> %{QUERY_STRING}
I edited the code accordingly for the first .htaccess-file
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING} [L,R=301,QSA]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}%{QUERY_STRING} [L,R=301,QSA]
If you change your second .htaccess file to the following, it should be clean :)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/i/image.php
RewriteRule ^/i/(.*) /i/image.php?img=$1 [P,NC,L,QSA]
The [P] stands for 'proxy' meaning that your Apache is seen by the client as a proxy (ofc, without his notice), because your Apache executes the actual (correct) request, while the client only sees the result.
My new take on your problem
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteRule ^(.*)$ https://%2$1 [R=Permanent,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^=]+)$ image.php?img=$1 [NC,L]

Htaccess Redirects - Combine many rules in one single redirect

I am trying to combine more rules in a single redirect, right now I have many rules and many redirects, like in the picture attached, but for SEO purpose this is not a good behavior.
The story is this: I have an old url, which doesn't exist anymore and a new one, where I want to be redirected. If the requested url doesn't have www. and https then I want to add them.Also if the url has an slash at the end, I want to remove it.
All is working right now, but in many steps.
This is what I have in my .htaccess file:
301 redirect from old url to the new one with www.
RewriteRule ^source1/source2$ https://www.domain.com/destination1/destination2 [L,R=301]
Remove the slash from the end of URL
RewriteCond %{REQUEST_URI} !^/system [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1 [R=301,L,QSA]
Redirect to https
#-----------------redirect to https-----------------
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www.domain.com(.*)$ [NC]
RewriteRule (.*) https://www.domain.com%{REQUEST_URI} [L,R=301]
I use all of these rules inside this directive:
<IfModule mod_rewrite.c>
Have it like this to avoid multiple 301 for SEO purpose:
# specific redirects
RewriteRule ^source1/source2/?$ https://www.domain.com/destination1/destination2 [L,R=301,NC]
# Remove the slash from the end of URL
RewriteCond %{REQUEST_URI} !^/system [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ https://www.domain.com/$1 [R=301,L,NE]
#-----------------redirect to https-----------------
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache completely before testing this change.

htaccess rewrite from a php file to subdomain

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]
Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html