Viewed   62 times

I have a website, and when i display any page the url will change to: https://www.asdgsdgsd.nl/index.php or about.php or contact.php etc...

I have seen that you can create 'Clean urls' with .htaccess, but there the links were: https://www.asdgsdgsd.nl/index.php?page=$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!!

 Answers

4

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.

Update:

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 http://www.pets.com/pet_care_info_07_07_2008.php

To look more like this: http://www.pets.com/pet-care/

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 https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

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
5

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
 
4
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:

http://domain.com/about/
http://domain.com/about
http://domain.com/contact/
http://domain.com/contact

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

http://domain.com/about/
http://domain.com/contact/

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
4

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:

.htaccess

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

game.php

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" ); 
  }
  exit;
}

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
 
1

With generate url use like that (see more http://www.yiiframework.com/doc-2.0/guide-helper-url.html):

Html::a('<b>Register</b>', 
        ['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:

story/create/39/11

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 :