Archive

Posts Tagged ‘mod_rewrite’

Forcing ssl to particular page and non-ssl to other pages

March 14th, 2013 2 comments

In projects we may need to make sure some of the urls are only accessible from ssl (https). It’s really useful for user sections. And rest of the url should browse from non-ssl (http) link. It can be done with a small htaccess rules.

Here is htaccess hack to do:

#SSL for some pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC]
RewriteCond %{REQUEST_URI} ^/(signin|signup|account) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

In this block we are checking for ssl off and some url, if match we redirect the user to same url in ssl version.


#SSL off for some pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC]
RewriteCond %{REQUEST_URI} !^/(signin|signup|account) [NC]
RewriteCond %{REQUEST_URI} !^/(api) [NC] #allow this url from both ssl and non-ssl
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

In this block we are checking for ssl on and some url, if match we redirect the user to same url in non-ssl version. Here u may notice we are using a extra check ( RewriteCond %{REQUEST_URI} !^/(api) [NC] ) , it’s because in project we may need some url those accisable from both ssl and non-ssl.

how to enable mod_rewrite in apache2.2 (debian/ubuntu)

July 15th, 2007 323 comments

Here i am going to describe how to enable mod_rewrite in apache2.2 -specaily for debian.

In default installion of apache2.2 on debian never enable mod_rewrite default. So you may need to enable .

First install the apache2.2 with this command :
debian user please use “su” before start this process
ubuntu user please use “sudo su” before start this process

apt-get install apache2 (it will install apache 2.2)

on current debian system you can enable mod_rewrite with simple command

a2enmod rewrite

old style, you can skip this portion

now use locate to find if the mod_rewrite.so is availble on your server

updatedb
locate mod_rewrite.so

it will found in “/usr/lib/apache2/modules”

new apache follow some folders to enable and disable mods.
so now do this:

cd /etc/apache2/mods-enabled
touch rewrite.load
gedit rewrite.load (you may use any editor to edit this file)

now paste this following line

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

end of old style

Then edit /etc/apache2/sites-available/default or /etc/apache2/sites-available/000-default (check which one available on your system)

Find the following

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

and change it to

Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all

and finally restart Apache

/etc/init.d/apache2 restart
or
service apache2 restart

OK, you done 😀

don’t forget to comment, if it works or not.

thanks.

if you like my post, please tweet it, so other also can see this post.

57 queries in 0.113 seconds