Viewed   62 times

I have a website, and when i display any page the url will change to: or about.php or contact.php etc...

I have seen that you can create 'Clean urls' with .htaccess, but there the links were:$1.

is it possible for me to create clean urls?

i have tried this in a .htaccess document on my root folder:

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ index.php

Some one who can help me please?? you need some more information? just ask and i wil answer it!!



You have the idea by using the rewrite rule, but you have missed out of the last bit.

RewriteRule ^([a-zA-Z0-9]+)$ index.php?id=$1

The $1 will take the first varable entered so you can have /index/example and the word example can be pulled as a _get['id']

You can create multipule in this way:

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?id=$1&login=$2

and so on.


If you just want to make the URL look nicer follow this link and read up on all the different ways of using the rewrite rule.

You can change

To look more like this:

If thats what you are looking for then the rule you want to use is:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^pet-care/?$    pet_care_info_01_02_2008.php    [NC,L]    # Handle requests for "pet-care"

Quoted from

The "RewriteRule" line is where the magic happens. The line can be broken down into 5 parts:

RewriteRule - Tells Apache that this like refers to a single RewriteRule.

^/pet-care/?$ - The "pattern". The server will check the URL of every request to the site to see if this pattern matches. If it does, then Apache will swap the URL of the request for the "substitution" section that follows.

pet_care_info_01_02_2003.php - The "substitution". If the pattern above matches the request, Apache uses this URL instead of the requested URL.

[NC,L] - "Flags", that tell Apache how to apply the rule. In this case, we're using two flags. "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used.

# Handle requests for "pet-care" - Comment explaining what the rule does (optional but recommended)

Wednesday, October 19, 2022

Specify [QSA] (Query string append) so you may pass a query string after your url.

RewriteEngine On
RewriteRule ^book/([^/]*).html$ book.php?title=$1 [QSA,L]

PS: Why are you using * here? Wouldn't + suit better?

Saturday, November 5, 2022
Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# If folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# and file exist
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# uncomment the below rule if you want the "/" to be required 
# otherwise leave as is
# RewriteRule ^([^/]+)/$ $1.php [L]
# internally show the content of filename.php
RewriteRule ^([^/]+)/?$ $1.php [L]

The above rule will:

  1. will not redirect if a folder exist
  2. will not redirect if the file does not exist
  3. will redirect what comes before the / if one is present as the file name

So it will work for all these examples:

If you want you can remove the ?, like the commented rule, to make it accept only URL's that end with a /.

Now these are important step for the above to work:

  1. It must go into the .htaccess on your root folder for example /home/youraccount/public_html/.htaccess
  2. The Options before the rewrite rule are very important specially -MultiViews
  3. The file must exist on the same place the .htaccess is for example in your case the about.php file
  4. The PHP must be working obviously.
Friday, December 16, 2022

You have to check if it was clean request or not. Otherwise you will fall into infinite loop

Here is an example from one of my projects:


RewriteEngine On
RewriteRule ^game/([0-9]+)/ /game.php?newid=$1


if (isset($_GET['id'])) {
  $row = dbgetrow("SELECT * FROM games WHERE id = %s",$_GET['id']);
  if ($row) {
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    Header( "Location: /game/".$row['id']."/".seo_title($row['name'])); 
  } else {
    Header( "HTTP/1.1 404 Not found" ); 

if (isset($_GET['newid'])) $_GET['id'] = $_GET['newid'];

So, you have to verify, if it was direct "dirty" call or rewritten one.
And then redirect only if former one.
You need some code to build clean url too.

And it is also very important to show 404 instead of redirect in case url is wrong.

Friday, August 12, 2022

With generate url use like that (see more

        ['story/create', 'id' =>39,'usr'=>'11'], 
        ['class' => 'profile-link'])

In urlManager input new rule:

rules' => array(
  'story/create/<id:d+>/<usr:d+>' => 'story/create',


Output url will be like that:


And in controller:

public function actionCreate($id, $usr)

And Yii2 provide this parameter.

Monday, December 5, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :