How to create Clean URL for every page using .Htaccess?

3.1k views Asked by At

I'm currently in the progress of creating a huge website, but instead of the regular URLs I'd like to use Clean / User Friendly URLs. I have been searching on how could I basically tailor these Apache Mod Rewrite rules for my needs, howere I could not found any solution for my particular problem.

Below you can read the aim, which I'd like to achieve with the URLs (I'm not going to write the domain name each time, just imagine: http://www.example.com ahead of the URL parts).

/register/ OR /register ---> /register.php (It should support both of the variations.)

I actually have more files for the registration and I'd like them to be accessible using the "part" words like:

/register/part1/ OR /register/part1 ---> /register.php?part=1 (It should support both of the variations.)

Also, what if I have more than just one query varialbe? (Like "personal=1")

/register/part1/personal/ OR /register/part1/personal ---> /register.php?part=1&personal=1

And what if I have many more of these queries, but I CAN'T specify all of them before? Any of these can be entered. (Like "thing,name,job,etc")

/register/part1/personal/Nicky/ OR /register/part1/personal/Nicky ---> /register.php?part=1&personal=1&name=Nicky

OR any kind of variations you can imagine:

/register/part1/personal/thing/employee/ OR /register/part1/personal/thing/employee ---> /register.php?part=1&personal=1&thing=1&job=employee

EDIT: This is what I've tried yet, but it just redirects the pages to index.php :/

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

So I have given you a lot of examples, what I'd like to basically achieve. I can have a lot of other pages besides "register.php" so it shouldn't be specific to that page only. I also want that which is VERY important, that IF someone goes to for example: register.php?part=1 it should redirect them to the appropriate Clean URL (of course in PHP).

I would also want to ask what should I do in the PHP end to make everything good? I saw that Wordpress has a really great solution for this, which is pretty automatic, and it looks great!

Is there any ways that someone could please explain me how to create a great .HTACCESS mod_rewrite solution for this? I would be really-really glad!

Please do not mark this question as duplicate, because I really did not found anything specific for my case.

2

There are 2 answers

0
nice ass On BEST ANSWER

You mentioned WordPress, which has something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

What this does is redirect any request that doesn't match a real file or directory to the index.php script. That script will then decide what page to display. It will do so by looking into $_SERVER['REQUEST_URI'] which holds a path like register/part1.

It would be easier for you to figure out what page to show using this method, because in PHP there are many ways to parse that path string, then map it to a function

0
Severin On

You should be able to construct clean URLs like this from your htaccess:

RewriteEngine On
RewriteRule ^index\.html$ /index.php?pageID=Home [L]
RewriteRule ^about-my-homepage\.html$ /index.php?pageID=About [L]
RewriteRule ^contact-us\.html$ /index.php?pageID=Contact [L]

the first is the one you want to output (the "clean" URL), the second one the one you actually want to open. Good Luck!