Fix IP and URL canonicalization issues on my .htaccess - .htaccess

Am trying to do an IP and a URL canonicalization on my .htaccess for my website(www.mydomain.com.ng) and IP(http://173.254.30.129/) i did the below code but it did not work.
RewriteEngine On
Options -multiviews
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_HOST} !^www.mydomain.com.ng$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com.ng/$1 [L,R=301]
RewriteRule ^index$ index.php [NC,L]
I did the above code on my .htaccess and i visit my URL(mydomain.com.ng) it did not redirect to this (www.mydomain.com.ng).
RewriteEngine On
Options -multiviews
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_HOST} ^173\.254\.30\.129
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
RewriteRule ^index$ index.php [NC,L]
I did the above code on my .htaccess i visit this IP(http://173.254.30.129/) it does not redirect to this (www.mydomain.com.ng).
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
also i want to do the above code on my .htaccess but do not know want to test for if it work.
Please help me with this three issue with am trying to achieve in my .htaccess Thank you.

not sure if you still have this issue seeing as this question is over 6 months old, but here are the codes that work for me:
Redirecting the url:
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Redirecting the IP address:
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
Your last piece of code is to avoid attacks by botnet scripts that automatically look for vulnerabilities in your software.
They are sometimes identified as User-Agent libwww-perl, and that's why you are supposed to disallow access from User-agent Libwww-perl.
As to how to test that I'm not sure of unfortunately.

Related

htaccess drop php for clean SEO friendly url

I'm having trouble dropping the file extension of my webpages. I have a static site of around 10 php files. I do not have any query parameters in the url. I'd like to have SEO friendly urls.
mysite.com/other-errors instead of mysite.com/other-errors.php
I've been searching but none of the entries in my htaccess file are working. This is what i've tired, amongst other version:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_HOST} ^mysite.co.uk [NC]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [L,R=301,NC]
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I know I can go the other way:
RewriteRule ^home index.php [NC,L]
But I'd have to do this for every php file, which is a little time consuming. This must be possible. Thanks in advance.
I seem to have solved my problem I needed one of the other solutions
I tried with my redirects. Is this the correct way?
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^mysite.co.uk [NC]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [L,R=301,NC]
# Redirect HTTPS to HTTP
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Permalink
RewriteRule ^home index.php [NC,L]
RewriteRule ^e02-error e02-error.php [NC,L]
RewriteRule ^other-errors other-errors.php [NC,L]
RewriteRule ^setup-and-chemical-balance setup-and-chemical-balance.php [NC,L]

Trying to use .htaccess for www and https redirects

I'm trying to get my site to redirect to HTTPS, and I can only seem to get it partially working.
Here's what my .htaccess currently looks like:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
The problem is, is that it's not retaining the URL (virtual path that gets parsed by index.php), so for example, http://blah.com/mypage redirects to https://blah.com
What am I doing wrong?
I also need to ensure www is in place as well, but one step at a time :)
You can use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blah.com$
RewriteRule ^$ https://blah.com/mypage [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^https://blah.com$
RewriteRule ^$ https://blah.com/mypage [L,R=301]
So my file works - I just didn't clear my cache. Major oops on my end.
Here's the .htaccess file for those curious:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

Redirecting to homepage htaccess issue

I have a strange issue where pages are redirecting to the homepage. Let me explain - my website is thedj.com.au
Now when I type in www.thedj.com.au/payments it redirects to thedj.com.au (even though it should be going to the page https://thedj.com.au/payments).
Any idea why this is and how to fix?
My htaccess file is below:
# BEGIN HTTPS Redirection Plugin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^home.htm$ https://thedj.com.au/ [R=301,L]
RewriteRule ^photos.htm$ http://photos.thedj.com.au/ [R=301,L]
RewriteRule ^contacts.htm$ https://thedj.com.au/contact-us/ [R=301,L]
RewriteRule ^booking.htm$ https://thedj.com.au/book-dj/ [R=301,L]
RewriteRule ^downloads.htm$ https://thedj.com.au/downloads/ [R=301,L]
RewriteRule ^payonline.htm$ https://thedj.com.au/payments/ [R=301,L]
RewriteRule ^price.htm$ https://thedj.com.au/pricing/ [R=301,L]
RewriteRule ^questions.htm$ https://thedj.com.au/faq/ [R=301,L]
RewriteRule ^links.htm$ https://thedj.com.au/links/ [R=301,L]
RewriteRule ^thankyous/index.htm$ https://thedj.com.au/testimonials/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://thedj.com.au/ [L,R=301]
</IfModule>
# END HTTPS Redirection Plugin
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^mrdj\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mrdj\.net\.au$
RewriteRule ^/?$ "https\:\/\/thedj\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^mrdj\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mrdj\.com\.au$
RewriteRule ^/?$ "https\:\/\/thedj\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^thedjs\.com\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.thedjs\.com\.au$
RewriteRule ^/?$ "https\:\/\/thedj\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^theperthweddingdjs\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.theperthweddingdjs\.com$
RewriteRule ^/?$ "https\:\/\/thedj\.com\.au\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^thedjs\.net\.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.thedjs\.net\.au$
RewriteRule ^/?$ "https\:\/\/thedj\.com\.au" [R=301,L]
Note:
I've tried commenting out the lines
#RewriteCond %{HTTPS} off
#RewriteRule ^(.*)$ https://thedj.com.au/ [L,R=301]
However this has not fixed the issue. I'm thinking this may perhaps be because the website is entirely https:// - but any more ideas would be much appreciated.
Best,
Kosta
The .htaccess shouldn't do any redirect here.
For testing, this website could be useful.
Is this configuration from your live site? Because https://thedj.com.au/payments works for me.
Browsers tend to cache permanent redirects quite aggressively. You need to clear the complete cache or use the private browsing mode to test a redirect again. (But you need to close ALL private browsing windows and then open a new one)

Excluding specific pages from HTTPS in mod_rewrite

I am trying to setup a codeigniter app to force HTTPS across all pages except one. However, I cannot get the rules to only redirect if the user is not on the page in question.
The page that should be excluded has the following URL's:
http://mydomain.com/kpi/reports/67
http://mydomain.com/kpi/reports/67/overview/2013-02-01/2013-02-28
http://mydomain.com/index.php?/kpi/reports/67
http://mydomain.com/index.php?/kpi/reports/67/overview/2013-02-01/2013-02-28
The number 67 and the dates can all change in the URL's above hence the user of regular expressions below.
I have tested the regular expressions and they seem to match the URL's fine. However, the htaccess just seems to redirect it to https:// anyway.
My .htaccess file is as follows...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Disallow access to system dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Disallow access to application dir
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/index\.php\?/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)$
RewriteCond %{REQUEST_URI} !^/kpi/reports/?([0-9]+)/overview/?([0-9]+)-?([0-9]+)-?([0-9]+)/?([0-9]+)-?([0-9]+)-?([0-9]+)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Any help would be greatly appreciated!
Maybe this will do what you need:
#Force https when not on overview report
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{QUERY_STRING} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/?$ [NC]
RewriteCond %{REQUEST_URI} !kpi/reports/[0-9]+/overview/[^/]+/[^/]+/? [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]
#If not a valid file, redirect request through index.php
Replace all lines between the comments.

Remove www site-wide, force https on certain directories and http on the rest?

Firstly, I would like to remove the www. from my domain name
http://www.example.com => http://example.com
I would also like for certain directories to be secure (https), while the rest remain http
http://example.com/login => https://example.com/login
Obviously it needs to work for all conditions, for example if someone types in:
http://www.example.com/login => https://example.com/login
Also when people navigate away from the login page it returns to http. Currently in the .htaccess is the following which is done automatically by the software I am using and I suppose needs to be there for it to work:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
Any ideas on how I can achieve dream control all these things?
Thanks in advance!
===================
#Gumbo Following your advice this is my complete .htaccess
RewriteEngine On
# remove www from host
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
# force HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(login)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force HTTP
RewriteCond %{HTTPS} =on
RewriteRule !^(login)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
Options -Indexes
Navigating to http://example/login still goes to http://example/index.php when it should go to https://example/login Do I have the order wrong?
Try these rules:
# remove www from host
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
# force HTTPS
RewriteCond %{HTTPS} =off
RewriteRule ^(login|foo|bar|…)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force HTTP
RewriteCond %{HTTPS} =on
RewriteRule !^(login|foo|bar|…)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You also might want to add some additional conditions to only change the protocol on GET and HEAD requests but not on POST requests.
This code works for me:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
remove www (tested):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]
redirect (not tested):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \/login/?.*\ HTTP [NC]
RewriteRule ^(.*)$ https://yoursite.com/login [L,R=301]
After reading this all post time line i have get success for following error: My website(c4dprime.com) was attached https: in mozilla firefox browser. Google and internet explore are show domain address as http: (The real problem in firefox) I did not want https: for my domain in any browser because one unknow redirect attached with my domain by https: (Cause for this error)after Installing following plugins in wordpress,(Easy redirect for ssl).... I have told to every one do not use any low level plugins for wordpress.
Anyway! After reading many articles and tutorials at this topic via google search engine. Now i am happy to say i have solve this problem from this post tips. Remember one thing about me i am new to in this form and wordpress.
This tip is help full for me
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://yoursite.com/$1 [L,R=301]
After edit or some changes in my .htaccess file following code i have use now with solved this error.
AddHandler c4d-prime .com
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteBase /
RewriteRule ^$ http://www.c4dprime.com/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Resources