Categories
Apache mod-rewrite and htaccess related Articles htaccess

htaccess redirect http to https for Cloudflare users | URL Rewriting Tips

Cloudflare http to https using RewriteRule

Cloudflare has its own variable to check URL scheme. If you are using cloudflare then your normal htaccess http to https redirect can fail to work. One possible reason behind this is that cloudflare uses %{HTTP:CF-Visitor} variable to check the requested URL scheme while on Apache we use %{HTTPS} for the same purpose.
To enforce SSL on a cloudflare enabled domain, the correct working rule is :


RewriteEngine on
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

You just need to replace RewriteCond %{HTTPS} off with RewriteCond %{HTTP:CF-Visitor} ‘”scheme”:”http”‘ in your existing code to make it work.

Categories
Apache mod-rewrite and htaccess related Articles htaccess url-redirection

A simple rule to redirect non-www domain to www with htaccess | URL Rewriting Tips

Enforce www using RewriteRule in htaccess

htaccess redirect non-www to www

In this post I will show you how to redirect a non-www domain to www using RewriteRule directive in htaccess or server.config files.
The following is a simple and basic rule to enforce www on a domain. You can add this to your htaccess in document root folder.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [NC,L,R=301]

You will need to replace “example.com” with your own domain name to use this rule.
This will redirect example.com and all its paths to www.example.com with whatever URI was requested.
Why I call it simple is because you just need to replace the domain name and code becomes fully functional on your domain. The syntex of the rule is easier to understand.


Here is line by line explanations of the rule :
RewriteCond %{HTTP_HOST} ^example.com$ [NC] This line checks to see if the host header is exactly example.com if yes.. then the condition becomes true and sends a green signal to the rule.
RewriteRule (.*) https://www.example.com/$1 [NC,L,R=301] The RewriteRule lines executes only if the RewriteCond is true. The pattern (.*) matches all URIs of “example.com” domain and redirects them to “www.example.com” . The $1 is a backrefrence. This includes the value captured in regex (.*) , for example: when the request is “example.com/foobar” the value the regex capture-group (.*) will capture is foobar and it will be saved in $n so that it can be used in substitution string.
[NC,L,R=301] are the mod rewite flags.
L means Last. This tells server to stop reading more rules if this one matches.
NC flag is used for ignoring character case. With this flag you can use both upper-case and lower case characters in URIs.
R=301 flag tells server to make a permanent redirection of the URLs with 301 HTTP redirect status

Rule for server.config file

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com$1 [NC,L,R=301]

The only difference between the rule for htaccess and the server.config files is the “destination of the rule” . As you can see here we trimmed the / traling slash that was right after the domain name in the substitution string. The reason why it is removed is because server.config uses an absolute path starting with / in RewriteRule. So the (.*) in RewriteRule captures URIs with a leading slash. For example if the requested URL is “example.com/foobar” then the regex pattern in RewriteRule in server.config context will save /foobar in $1 variable.

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess mod-rewrite

Prevent hotlinking using htaccess

htaccess block hotlinking

Hotlink protection using htaccess

One of the most beneficial use of htaccess is to prevent hotlinking of images , videos and other documents on your website.

You can use htaccess to prevent hotlinking of your media and other documents.

What is hotlinking?

It means to display an image on a website by linking to the website hosting the image.
For example : An image “cat.jpg” hosted on server1 can be displayed on “server2” using src link to the server1 .
This uses the Server1 resources to load and display the image.

Why you should prevent hotlinking ?

Hotlinking consumes your server resource like bandwidth which can slow down your server performance.

If your hosting server provides limited bandwidth then “hotlink blocking” is useful for you as you do not want your server to be down by external http requests.

RewriteRule to prevent hotlinking

Many people who don’t want their site to be hotlinked use htaccess.

It’s easy to stop hotlinking using an htaccess file.

The following is a basic Rule to prevent image hotlinking on your website :

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://yoursite.com [NC]
RewriteRule .(jpg|png|gif)$ - [F]

This checks the “HTTP_REFRRER” domain string , if its not http://yoursite.com then any calls to jpg , png and gif files on your site will get a F 403 forbidden error.

Linking an image file on another server using your domain “http://yoursite.com” will show a forbidden error.
For example, <img src=”https://yoursite.com/image.jpg”> won’t load the image file on another server.

If yoursite.com is accessible by both https or www , then to prevent hotlinking you need to use a regex based pattern in RewriteRule to match both versions :

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)://(www.)?yoursite.com [NC]
RewriteRule .(jpg|png|gif)$ - [F]

This will prevent hotlinking of image files on “https://www.yoursite.com” , “https://yoursite.com” and “http://yoursite.com” (with or without www) .

Keep in mind that, hotlink protection doesn’t mean your images won’t be indexed by search engines. Google will still index your images as it doesn’t use http referer header to find and index files.
Your images will still appear on search results but can’t be used on another website by linking to your server.

Stop hotlinking of video files.

You can use the following rule to stop hotlinking of video files on your server.

I have just added one extension in the rule . If you have more video extensions to stop hotlinking you can add them in the pattern using regex like this .(mp4|ext2|ext3)$ .

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https?://(www.)?yoursite.com [NC]
RewriteRule .mp4$ - [F]

This will set a hotlink protection for mp4 files on your server.

htaccess Allow/Deny hotlinking

By using the above rules you deny hotlinking to all external sites. If you want to allow access to a specific external site that can use your media files then use :

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https?://(www.)?(example1.com|example2.com) [NC]
RewriteRule .(jpg|png|gif)$ - [F]

This will block image hotlinking for all sites except “example1.com” and “example2.com” . If the “example1.com” is hosting the image files then you can also hotlink those images on “example2.com” .

Display a static image for hotlinked requests

You can show a static image for hotlinking calls on your server using htaccess.
Instead of 403 error you can rewrite all hotlinking calls to an image file this will make it so that when someone tires to link your image on their website they will get the image instead of the one they intended to show.

This does use your bandwidth but this way you can reduce the number of hotlinking calls on your server.

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^https?://(www.)?yoursite.com [NC]
RewriteRule .(jpg|gif|png)$ /hotlink.png [L]

Hope this article was helpful.
Thanks for reading!

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

How to block google yahoo bing and other search engine bots using htaccess ? | mod-rewite tutorial | amitoverflow blog

Block search engine bots using htaccess

htaccess block search engines

With htaccess it is possible to block search crawler bots such as Google , yahoo and bing bot or other web crawlers from accessing your website.
Block search engine bots using htaccess

If you don’t want your website to be visible to search engines then you can easily deny access to web crawling bots using RewriteRule directive in an htaccess file .
In this article I will show you how to block search engines from crawling your website. We will use our htaccess file for this blocking.

htaccess code to block google, yahoo and bing bots

The following is a tested and 100% working code so you can just copy and paste it to your .htaccess file .

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} google|yahoo|bing [NC]
RewriteRule ^.*$ - [F]

This rule will show a forbidden (403) error for “search bots” when they try to visit any URLs on your website and won’t be able to crawl or index your website.
explanations of the code above :

  1. The first line RewriteEngine on turns the RewriteEngine on. This tells server to read RewriteRule(s) in htaccess file. You can remove this line if your htaccess already contains one and place the rule just bellow that because Only one RewriteEngine directive is needed per htaccess.
  2. The second line RewriteCond %{HTTP_USER_AGENT} google|yahoo|bing [NC] is a condition for the RewriteRule. Here we match against the “HTTP_USER_AGENT” string using a regular expression based pattern that matches those three bots. Since we dont know the exect user agent string so we can just use their name like “google” ,”yahoo” ,”bing” the pattern will automatically match the full user_agent string.
  3. The third line RewriteRule ^.*$ – [F] rewrites all requests to F it is short for forbidden or 403 error . If the condition above the rule is met (http_user_agent ===google OR yahoo OR bing) then this rule will get triggered.
    You can replace the F flag with R=403 or any other error status.
    The pattern (.*) means the entire site. If you want to block search crawler bots from reaching a specific file or path on your server then you can change the pattern accordingly ie ^demo.php$ applies only this specific file.

Block a specific user agent

To block a specific search bot for example googlebot,you can use the following:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} google [NC]
RewriteRule ^.*$ - [F]

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

How to get rid of Rewrite loop error in htaccess file ? Rewrite loop error solution

Htaccess how to fix RewriteLoop

How to fix RewriteLoop error in htaccess

RewriteLoop is when a file or URL path infinitely maps to itself using RewriteRule directive in htaccess.
Your webserver returns one of the following 500 error status when this error occur on the Apache server :

HTTP Error 500 by infinite loop

“The request exceeded the maximum 10 internal redirects”.

or

“Too many redirects. The server is unable to complete the request”.

If you check you error.log file you will see a 500 error message that looks something like :

[client 00.0.0.1:0000] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use ‘LimitInternalRecursion’ to increase the limit if necessary. Use ‘LogLevel debug’ to get a backtrace.

What causes RewriteLoop?

It happens when you rewrite a file or URL path to itself or when there are unconditional rules in htaccess with (.*) pattern. Let’s see some code examples to better understand the loop behavior in mod-rewite.

The following RewriteRule will cause an infinite loop error if you use it in your htaccess:

RewriteEngine on
RewriteRule ^(.*)$ /index.php [L]

The rule above rewrites all requests to index.php but using this rule in htaccess you will get the infinite loop error if you visit any URL path on your server.

Why does the above rule result in RewriteLoop error?It’s because the Rule runs unconditionally. The pattern ^(.*)$ in the above rule also matches the destination path /index.php and keeps rewriting the request in a never ending loop

/foobar =>index.php =>index.php =>index.php…..

With the above rule if you visit

http://yoursite.com/foobar

The rule’s pattern ^(.*) will match the request /foobar and rewrite it to /index.php but since htaccess file is read by server on each request , to load the destination file index.php your server will again read the htaccess file and this time /index.php matches the pattern and rewrites to itself.
Unlike server.config, htaccess is read by server multiple times in order to complete the request. So for example with the above RewriteRule if you visit /anyURI the rule will rewrite your request to /index.php . The processing doesn’t end here. The server will again read the htaccess in order to serve the /index.php URI and the rewriting process keeps going on untill there is no matching pattern.

How to quick fix the Rewrite loop ?

We will take the above example here. To fix the rewrite loop error on :

RewriteEngine on
RewriteRule ^(.*)$ /index.php [L]

You can either use a RewriteCond or END flag insted of L.
Keep in mind that the END flag only works on apache versions 2.4 or later .

On earlier versions you can use RewriteCond directive to solve the looping problem.

Using RewriteCond to fix the Rewrite loop

RewriteEngine on
RewriteCond %{REQUEST_URI} !index.php [NC]
RewriteRule ^(.*)$ /index.php [L]

The condition RewriteCond %{REQUEST_URI} !index.php [NC] prevents the destination path /index.php from matching the rule’s pattern.

You can also get rid of the loop using a regex pattern that excludes the destination path.
The main point here is to exclude the path we are rewriting to.
If you don’t want to use RewriteCond then you can simply use :

RewriteEngine on
RewriteRule ^((?!index.php).*)$ /index.php [L]

The pattern ^((?!index.php).*)$ will match all URIs except the destination index.php .

Using END flag to avoid the rewrite loop

As I alrealdy mentioned the END flag is supported only on Apache 2.4 and newer . If your apache version supports it you can use END flag in RewriteRule to terminate the loop. Replace all L flags you are using on RewriteRule with END.

RewriteEngine on
RewriteRule ^(.*)$ /index.php [END]

A global workaround for fixing Rewrite Loop

If your htaccess contains multiple rules then it can be a bit complicated to find and fix the rule causing infinite loop error . In that case you can usevthe following Anti loop protection rule at the top of your htaccess:

RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} ^200$
RewriteRule ^ - [L]

This will fix the Rewrite loop error in your htaccess file. The above rule will terminate all internal rewrite loop and redirections. It prevents other rules in your htaccess from executing when the second rewrite cycle starts on the server.
Now you can route all requests for your site to index.php without any errors :

RewriteEngine on
#anti loop protection
RewriteCond %{ENV_REDIRECT_STATUS} ^200$
RewriteRule ^ - [L]
#rewrite all to index.php
RewriteRule ^(.*)$ /index.php [L]

Hope this was helpful. Thanks for reading this.

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

Rewriting a complax and long URL using htaccess | mod-rewite tutorial

Rewriting a complax URL using htaccess

Rewrite long URL htaccess

In this post I will show you how to rewrite a long URL that has multiple query string perameters.
This two minutes read post is for beginners who wish to learn Apache URL rewriting.
We will convert the following URL :

http://example.com/user.php?name=foobar&location=somewhere&id=123

To

http://example.com/user/foobar/somewhere/123

Our original URL http://example.com/user.php?name=foobar&location=somewhere&id=123 contains three query string perameters name , location and id. The URL is long, hard to remember and not search engine friendly .

It needs to be converted into a clean format so that it can easily be indexed by search engines.

The new URL http://example.com/user/foobar/somewhere/123 is short , easier to remember and search engine friendly. It contains 4 path segments user , foobar , somewhere and 123 each points to the query string except the first one user as its there just for naming purpose.

Here is the rule we can use to convert our complex URL into short one

RewriteEngine on
RewriteRule ^user/foobar/somewhere/123$ /user.php?name=foobar&location=somewhere&id=123 [L]

The above rule create the short URL. It allows you to access the original query string URL using the new short URL. If you visit example.com/user/foobar/somewhere/123 the rule will internally map the URL to the original query string URL.

The rule above applies to a single Query string URL or static path. If the URL is dynamic you can use Regular expression based pattern instead of the static pattern.

RewriteEngine on
RewriteRule ^user/(.+)/(.+)/(.+)$ /user.php?name=$1&location=$2&id=$3 [L]

The regular expression pattern ^user/(.+)/(.+)/(.+)$ matches any URL path string starting with user followed by /name/location/id . The pattern matches /user/name/location/id . The $n variables reffers to the match in capture group (.+) where $1 is the match from the first group.
You can manually modify the rule if your URL has more then three perameters . For example if your original URL has 4 query perameters then you can add an additional capture group to the pattern and link the match to the destination path using $4 variable.


I hope you enjoyed reading this post. Thank you.

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

What is RewriteCond and how to use it ? Apache RewriteCond examples |amitoverflow blog

Apache RewriteCond directive

What is RewriteCond in mod-rewite

In this article I will give you a quick overview of what RewriteCond is and How it is used with RewriteRules.
RewriteCond is an apache mod-rewite directive which is used for conditional URL rewriting on Apache server.

We use RewriteCond along with RewriteRule%{HTTP_HOST} , %{HTTPS} etc.

RewriteCond syntex

RewriteCond testString conditionPattern flags

  • RewriteCond : RewriteCond directive
  • TestString : A server variables like %{HTTP_HOST} or plain text string like hello
  • Condition Pattern : A regular expressions based pattern.
  • Flags : Mod rewrite flags like [NC] and [OR] .

Understanding how RewriteCond actually works in mod-rewite

RewriteCond directive sets a condition for the rule to run. With RewriteCond you can rewite your URLs conditionally or when the specific condition is met.

Let’s see a basic example of how it actually works in RewriteRule.

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]

The RewriteCondion logic above can also be read as :

If http_host is www.example.com or
If http_host ==www.example.com

To apply this logic to RewriteRule, you can use :

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^file.php$ http://example.com/ [R]

The rule above will redirect /file.php to http://example.com/

if our condition is met or the host header is www.example.com .

RewriteCond directive takes three perameters Test string , Condition pattern and the third one is Flags .
The first two are required perameters and the third one Flags is optional.

The two supported flags that can be used in RewriteCond are NC and OR .

Mod Rewrite pre-defined variables you can use in RewriteCond directive.

Also
see a list of mod-rewrite variables .

mod-rewite variables such as %{HTTP_HOST} are used as test string in RewriteCond to rewrite URLs conditionally.
The following is a list of some commonly used variables:

  • %{HTTP_HOST} : The http_host header, your domain ie www.example.com .
  • %{REQUEST_FILENAME} : This variables contains the requested filename path relative to the server root.
  • %{REQUEST_URI} : The requested URI string ie. /path/file.php .
  • %{HTTPS} : contains the value off if requested scheme is http . If the scheme is https then this variables contains off value.
  • %{HTTP_REFERER} : http referer URL string ie. http://example.com .

Examples :

#1) Redirect all http URLs to https
To redirect all http URLs to https we will use the following logic :
“If https off” then redirect.


RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com/$1 [R,L]

This will redirect all http URLs to https. The rule above RewriteRule (.*) https://example.com/$1 [R,L] executes if the condition RewriteCond %{HTTPS} off is met or the condition is true. If you request a non-secure URL starting with http the rule will then redirect it to the https version.


#2) Redirect non-www domain to www.The logic we will use here is :
“If host non-www then Redirect”


RewriteEngine on

RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule (.*) http://www.example.com/$1 [R]

We use %{HTTP_HOST} variable to match against the current host header. The rule above will redirect your non-www domain to www. The regular expression !^www. matches http host string that doesn’t start with www. our Rewrite condition above checks if the host is not www. If the host string starts with www then the rule won’t trigger. The rule triggers only if you request example.com.

Using a plain text string as test string in RewriteCond

For testing purposes, you can also use a plain text based string insted of mod-rewite variables in RewriteCond.

RewriteEngine on
RewriteCond hello ^hello$
RewriteRule (.*) http://example.com/ [R]

The rule above will redirect all your site URLs to http://example.com if the condition is met. The RewriteCond above RewriteCond hello ^hello$ is same as “if hello ==hello”.

RewriteCond applies to a particular RewriteRule that immediately follows it

One of the most important things to notice about the behavior of RewriteCond is that it applies only to a single rule that immediately follows it . Your RewriteRule can have multiple RewriteConds but the only single rule that is placed bellow RewriteCond(s) is affected.
See the rule example bellow

RewriteEngine on
RewriteCond foo ^foo$
RewriteCond bar ^bar$
RewriteRule (.*) http://example.com/ [R]
RewriteRule (.*) http://google.com [R]

In the code above both conditions apply to the first rule. The second rule won’t get affected by the conditions. The rule will redirect all your site URLs to http://example.com if the condition is met.

The use of [OR] flag in RewriteCond

By default , RewriteCond(s) directive uses AND logic when combined together in a Rule block.
Consider the following rule


RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com [R]

The RewriteCondions in the rule above use AND logic ie : “if host ==example.com” AND host==example.com” . Both conditions must met in order for the rule to run.

You can change the condition logic to OR using [OR] flag between two or more conditions .


RewriteCond %{HTTP_HOST} ^www.server1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.server2.com$
RewriteRule (.*) http://example.com [R]

This rule uses OR based RewriteCondion logic ie: “if host ==www.server1.com” OR “host==www.server2.com” . Only one condition needes to be met in order to trigger the rule.

The use of [NC] flag in RewriteCond

NC (NoCase) flags is used to ignore the difference between upper case and lower case characters in the condition . You can check the test string in case insensitive manner using NC.


RewriteCond fooBar ^foobar

The condition above is “false” because the test string contains upper and lower case characters. You can sovle this using NC flag.


RewriteCond fooBar ^foobar$ [NC]

Now it’s perfact. The condition is “true”.
You can combine multiple RewriteConds using OR flag. Suppose you have two domains pointing to the same document root and you want to redirect www + non-www version of one of these two domains to a different location , instead of writing two separate rules for each domain you will use a single rule with RewriteConds combined with OR flag.
Here is an example:


RewriteEngine on


RewriteCond %{HTTP_HOST} ^www.server1.com$ [OR]
RewriteCond %{HTTP_HOST} ^server1.com$
RewriteRule (.*) http://diffrentServer.com/ [R]

The rule will redirect both www.server1.com and server1.com otherwise without the OR logic , we need to write two rule blocks for the redirection to work :



RewriteCond %{HTTP_HOST} ^www.server1.com$
RewriteRule (.*) http:diffrentServer.com/ [R]
RewriteCond %{HTTP_HOST} ^server1.com$
RewriteRule (.*) http:diffrentServer.com/ [R]

The rule above can also be shortened to one condition using Regex for example : RewriteCond %{HTTP_HOST} ^(www.)?server1.com$ .

I hope you enjoyed reading this post. Thanks for reading!

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

How to use htaccess to remove html file extension from urls -mod-rewite guide

htaccess remove html from URLs

htaccess rule to remove .html file extension from URLs

A 100% working and tested rule to trim .html from URLs .
You can just copy and paste it to your .htaccess or server.config files on Apache server.

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+).html [NC]
RewriteRule ^.+$ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?(.+)/?$ /$1.html [L]

The RewiteRule above will convert all your URLs from

http://example.com/file.html

To

http://example.com/file

If you want to add a traling slash (a / at the end) to your new non-html URL forexample

example.com/file/ then just put a traling slash at the end of the destination path of the first rule .

Change the first RewriteRule line to

RewriteRule ^.+$ /%1/ [R=301,L]

This will automatically force a traling slash on your html URLs and your URL example.com/fileName.html will get redirected to example.com/fileName/ .

Where to put the RewriteRule on Apache server

You can use it either in your .htaccess or server.config file.

In case you don’t have access to the main server.config file on your server then it’s ok to use it in htaccess file.
You can use the rule in your public_html/.htaccess so that it can apply to all folders and subfolders.

If you don’t want the rule to apply to a specific sub directory then create a seprate .htaccess file with the following single line contents
RewriteEngine on and save the file. Then your public_html/.htaccess will not affect the subdirectory.

Also see this : How to remove .php extension from URLs .

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

htaccess RewriteRule to deny access to opera mini browser |amitoverflow blog

htaccess block Opera browser

A simple rewrite rule to forbid requests from opera mini browser.
If your website is designed for well known browsers like Chrome, sfari etc and you don’t want visitors to access your website using Opera mini browser , you can use the following RewriteRule in your htaccess or server.config file to block access to this specific browser.


RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} opera [NC]
RewriteRule ^.*$ - [F]

This rule will redirect all requests from opera to your website to 403 forbidden error . Visitors who come to your website using opera will see a forbidden error.

Instead of showing a forbidden error ,if you want to redirect your opera visitors to a specific page on your site then you could use :


RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} opera [NC]
RewriteRule ^.*$ /demo.php [R]

This will redirect your opera visitors to /demo.php page on your site.

On your server you may have more then one domain pointing to the same document root. The rules above apply to all domains including subdomains if these are pointing to the same document root. In that case you can use a RewriteCond directive to match against the %{HTTP_HOST} header so that the rule can apply to a specific domain only.


RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} opera [NC]
RewriteRule ^.*$ - [F]

The rule above will show a forbidden error for opera visitors and the rule applies only to www.example.com . This won’t affect your other domains for example if you have www.example2.com using the same document root then this rule will not apply to your www.example2.com domain.

Categories
apache Apache mod-rewrite and htaccess related Articles htaccess

Redirect domain from http to https with Apache mod-rewite and htaccess| 100% working code examples you can just copy and paste

Redirect http to https htaccess

When we enable SSL on our domain the next important stap we need complete is http “urls redirection to https” .  We need to redirect all our http URLs to our current HTTPS scheme in order to maintain the SEO rank on search engines and to avoid SSL mixed contents warning that often appears in a web browser when a file loads on a mixed http and https connection.
With RewriteRule directive you can easily redirect all your http URLs to https with just one 301 redirection. 

http to https URL redirection using RewriteRule  directive

You can use the following rule in your server.config or htaccess file to redirect your non secure http URLs to secure Https version on your server.
This is a 100% working rule so you can just copy and paste it to your server.

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI} [NE,L,R=301]

The rule above will redirect

http://example.com

To

https://example.com

The rule above is a basic version of RewriteRule to enforce https on all domains pointing to your document root. 

If you want to redirect a particular domain to https then you may use the following:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ [NC]
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301,NE]

This will redirect only a single domain ie www.example.com to its https version. The regex pattern in the RewriteCond  ^(www.)?example.com$ means to match either www.exaple.com or example.com . So this applies to both www and the naked version of your domain.

Redirect domain to https and www

You can redirect your domain to https with www  with the following rule :

RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^(www.)?(.+)$
RewriteRule (.*) https://www.%2%{REQUEST_URI} [NE,L,R=301]

The combination of “OR” and “AND” rewrite conditions makes it possible to redirect all versions of http to https://www .

This will redirect all versions of your http and https to https://www
for example:

http://example.com http://www.example.com https://example.com

To

https://www.example.com