Thursday, December 11, 2008

Hide .php from users to make your url more friendly

I have written an article named "How to rewrite URL in apache".
Today i am sharing with my readers and PHP programmers.
First of all we will check that either mod_rewrite in apache load modules is enabled or NOT.
In Linux openSUSE:
By default, openSUSE doesn't enable the mod_rewrite rewrite module. It's installed, but not enabled.
So lets enable it,
1. Edit the file /etc/sysconfig/apache2 as root:
i. search for APACHE_MODULES, you should find a line like this
APACHE_MODULES="suexec access actions alias auth auth_dbm autoindex cgi dir env expire include log_config mime negotiation setenvif userdir ssl php5"
ii. Add 'rewrite'(without quotes) to the content in the list between the "".
iii. Save the changes and quit
2. run SuSEconfig to update the apache configuration files.(From terminal)
3. run /etc/init.d/apache2 restart to restart the Apache server.(From terminal)
To check either mod_rewrite is enabled or not. phpinfo();, write in editor, save it in the root directory and open it in browser.
Search for word "mode_rewrite"(without quotes). If you find it, it mean rewrite is enabled. if not, then check the steps again and try.

Now you have to do some tweaking of default-server.conf file.
1. Edit the file /etc/apache2/default-server.conf with your prefered editor.
i. Search for AllowOverride - it should be below the line Directory "/srv/www/htdocs"
ii. Change AllowOverride None to AllowOverride All - this will allow custom .htaccess rewrite rules.
iii. Save your changes and exit
2. run SuSEconfig to update the apache configuration files.
3. run /etc/init.d/apache2 restart to restart the Apache server.

Now every thing is fine and running, lets test it. that it is working or not.
Create a directory with any name, i create mod_rewrite, open editor and write the following code there.

if($_GET['link']==1){echo"You are not using mod_rewrite";}
elseif($_GET['link']==2){echo"Congratulations!! Your Apache mod_rewrite test is successful";}
else{echo"Linux Apache mod_rewrite Test";}

//make it url as by href method.
"rewrite.php?link=1"LINK1 = rewrite.php?link=1
"link2.html"LINK2 = link2.html

save it as rewrite.php

Now write:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L]

and save it as .htaccess in the same directory.
Open rewrite.php in browser and click on link1, and then on link2. If you see the message "Congratulations!! Your Apache mod_rewrite test is successful", means that, its working.

In WINDOWS:
How to enable mod_rewrite module in apache in xampp, wamp?

1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.
4) Now restart the apache server.
5) You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.



The rest of the testing is same as above.

ANL

1 comment:

Anonymous said...

well this really make ur url friendly. But it will raise eyebrows.