PDA

View Full Version : [Tut] Mod-Rewrite (.htaccess)



Eccentric
06-10-2007, 09:19 PM
Think your links look ugly eg.
www.yoursite.com/index.php?page=test&skin=blue&language=eng&time=uk (http://www.yoursite.com/index.php?page=test&skin=blue&language=eng&time=uk)

Well .htaccess has made it alot easier for you to change this to something like;
www.yoursite.com/test/blue/eng/uk/ (http://www.yoursite.com/test/blue/eng/uk/)

What is .htaccess?

.htaccess (hypertext (http://en.wikipedia.org/wiki/Hypertext) access) is the default name of Apache's (http://en.wikipedia.org/wiki/Apache_HTTP_Server) directory (http://en.wikipedia.org/wiki/Directory_%28file_systems%29)-level configuration file (http://en.wikipedia.org/wiki/Configuration_file). It provides the ability to customize configuration for requests to the particular directory.
.htaccess files are commonly used for:

Authorization (http://en.wikipedia.org/wiki/Authorization), authentication (http://en.wikipedia.org/wiki/Authentication): .htaccess files are often used to specify the security restrictions for the particular directory, hence the filename "access". The .htaccess file is often accompanied by an .htpasswd file which stores valid usernames (http://en.wikipedia.org/wiki/Username) and their passwords (http://en.wikipedia.org/wiki/Password).
Customized error responses: Changing the page that is shown when a server-side error occurs, for example HTTP 404 Not Found (http://en.wikipedia.org/wiki/HTTP_404).
Rewriting URLs (http://en.wikipedia.org/wiki/Rewrite_engine): Various server-side PHP (http://en.wikipedia.org/wiki/PHP) scripts use .htaccess to rewrite "ugly" URLs to shorter and prettier ones.Taken from; http://en.wikipedia.org/wiki/Htaccess

Here's how you can do it.
So first of all we turn on and make sure all the links follow.

Options +FollowSymLinks
RewriteEngine OnNow this is where it gets hard, lets just test with one,

RewriteRule ^([A-Za-z0-9-]+)/$ index.php?page=$1 [L]this will change yoursite.com/index.php?page=test to yoursite.com/test/ (remember the last / ).

Now we will do the rest,

RewriteRule
^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?page=$1&skin=$2&language=$3&time=$4 [L]

You will need to save that in the appropriate directory.

Code breakdown,

([A-Za-z0-9-]+)This allows it to either be UPPERCASE lowercase or BoTh or numbers (0-9)
you can allow it just for numbers,

([0-9]+)
page=$1the $1 means that its the first in the url, like yoursite.com/first/second/third :) i think thats all enjoy.

OR you could use RichardKnox method which is also very good and easier to use thanks :),

You overcomplicated it.

http://www.yoursite.com/index.php?pa...ge=eng&time=uk

to

www.yoursite.com/test/blue/eng/uk/ (http://www.yoursite.com/test/blue/eng/uk/)

doesn't require as much as you;ve put. Try

Code:
RewriteEngine On

RewriteRule (.*)/(.*)/(.*)/(.*)/ index.php?page=$1&skin=$2&language=$3&time=$4

UPDATE:
Iv had a few PMs asking me how to access .htaccess so heres how:
Log into Cpanel and click on "File Manager". You should then get a pop-up asking which directory to open, if you look a bit further down on the pop-up you should see two Check boxs. Check "Show Hidden Files" and click "Go".
Now go to www if your not allredy in there, and you should see .htaccess :)
[ Credit to .:Jack120:. ]

Edited By Sunny. (Forum Moderator): Updated as requested.

RichardKnox
06-10-2007, 09:24 PM
You overcomplicated it.

http://www.yoursite.com/index.php?pa...ge=eng&time=uk

to

www.yoursite.com/test/blue/eng/uk/

doesn't require as much as you;ve put. Try



RewriteEngine On

RewriteRule (.*)/(.*)/(.*)/(.*)/ index.php?page=$1&skin=$2&language=$3&time=$4

But, well done.

Eccentric
06-10-2007, 09:26 PM
You overcomplicated it.

http://www.yoursite.com/index.php?pa...ge=eng&time=uk

to

www.yoursite.com/test/blue/eng/uk/ (http://www.yoursite.com/test/blue/eng/uk/)

doesn't require as much as you;ve put. Try



RewriteEngine On

RewriteRule (.*)/(.*)/(.*)/(.*)/ index.php?page=$1&skin=$2&language=$3&time=$4But, well done. Oh sorry, i've never came across that way as i've always used the way i posted, but if it works then yeh use it :) Thank you richard.

RichardKnox
06-10-2007, 09:27 PM
Well (.*) is a wildcard. Meaning (.*) can be substitued for any value, in the case of $1 "test".

Eccentric
06-10-2007, 09:29 PM
so it allows anything wouldnt that be a bit unsafe specially using mysql or.. so you could use a mysql attack like that where as if you only allow certain characters?

RichardKnox
06-10-2007, 09:30 PM
Well no, not really. Cause if your using it, you should be cleaning your GET url's anyway.

Eccentric
06-10-2007, 09:31 PM
true true. Thanks.

Moh
06-10-2007, 09:39 PM
what if I wanted to change:
/pages/PAGEGROUP/PAGENAME
to:
/PAGENAME

Do I have to add them all manualy? The groups that is

Eccentric
06-10-2007, 09:42 PM
could you explain im a little confused, what is the url so far?

RichardKnox
07-10-2007, 10:03 AM
No, you use (.*)

Moh
07-10-2007, 02:54 PM
Iv had a few PMs asking me how to access .htaccess so heres how:
Log into Cpanel and click on "File Manager". You should then get a pop-up asking which directory to open, if you look a bit further down on the pop-up you should see two Check boxs. Check "Show Hidden Files" and click "Go".
Now go to www if your not allredy in there, and you should see .htaccess :)

Eccentric
07-10-2007, 03:52 PM
Iv had a few PMs asking me how to access .htaccess so heres how:
Log into Cpanel and click on "File Manager". You should then get a pop-up asking which directory to open, if you look a bit further down on the pop-up you should see two Check boxs. Check "Show Hidden Files" and click "Go".
Now go to www if your not allredy in there, and you should see .htaccess :)
Yes i forgot about cpanel 11 as im using cpanel 10 :P cos my host but yes that would be how you do it if a mod can add this to the orginal post giving jack credit too please.

Want to hide these adverts? Register an account for free!