Hi, i'm trying to do clean url's using .htaccess but i'm having problems.

Here i have my .htaccess:

<IfModule mod_rewrite.c>

    RewriteEngine on

    RewriteRule ^([a-z]+)\/([0-9]+)\/?$ parameter_letter_and_number.php?param=$1&param2=$2 [NC]

    RewriteRule ^([a-z]+)\/?$ $1.php [NC]

    RewriteRule ^([0-9]+)\/?$ parameter_number.php?param=$1 [NC]

    RewriteRule ^([0-9_]+)\/?$ parameter_number_and_underscore.php?param=$1 [NC]

</IfModule>

Here is my parameters page so i can test:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Clean URL tutorial with .htaccess and PHP by http://codeofaninja.com/</title>

        </style>
    </head>
<body>

<h1>Parameter: Letter and Number ONLY. This is parameter_letter_and_number.php</h1>
<?php 
echo "PARAMETER VALUE 1: " . $_REQUEST['param'];
echo "<br />";
echo "PARAMETER VALUE 2: " . $_REQUEST['param2'];
?>

</body>
</html>

it should appear somthing like this:
http://localhost/clean-urls/post/143

but in that way gives me 404 Error it works only if the url is http://localhost/clean-urls/parameter_letter_and_number.php?param=post&param2=143

Can someone help me please?

Thank You,
PF2G

Member Avatar for LastMitch

but in that way gives me 404 Error it works only if the url is

The url you provided doens't make sense.

If you want this:

http://localhost/clean-urls/post/143

You can try this:

RewriteEngine On
RewriteRule ^clean-urls/post-143$ /parameter_letter_and_number.php?param=\$1&param2=\$2 [L]

This code how did you came up with this one:

http://localhost/clean-urls/parameter_letter_and_number.php?param=post&param2=143

Are these parma url keys?

Nothing happened. Do you have another example that can help me.

This one is really confused, i don't understand it very much, and i never worked with .htaccess

Thank you,
PF2G

Member Avatar for LastMitch

Nothing happened. Do you have another example that can help me.

You didn't answer my question is this parma url keys? Which means it's echoing out url?

This one is really confused, i don't understand it very much, and i never worked with .htaccess

The url you provided doesn't look right so that's why I ask is the parma url keys.

I mean you can at least know where the data is coming from because it's on your database or server!

You can't expect someone to figure this out?

Member Avatar for diafol

Parma keys = Param keys ?

Member Avatar for LastMitch

Parma keys = Param keys ?

No, it's not Parameter keys. I'm not sure maybe until he clarify how it works but I think it look like this:

http://php.net/parse_str

but instead it's on his database.

So that's why I mention is it a url key.

Member Avatar for diafol

Parma keys just struck me as odd.

Maybe you could forward everything to a php file and have it parse the URL?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

First line enables the rewrite engine, 2nd, 3rd, and 4th check to see what is entered is an existing file or directory, then the last line forwards it to the PHP file.
A simple parser would contain something like this:

<?php
/* Fetches the get data containing the url information */
$url = $_GET['url'];
/* Splits it into an array on / signifying directories */
$url = explode('/',$url);
/* Loop through for it to do something */
foreach($url as $seg){
    /* Your code */
}

Here's mine which is much more complex.
So essentially .htaccess is taking everything after a specific portion of the URL and sending it to the index.php file through the url get variable. index.php picks up that get variable, splits it into an array on a /, simulating directories. After that you can loop through the array and load files accordingly etc... That may be a bit overkill and or confusing, hope I gave some ideas.

Do i have to configurate localhost (wamp) because it gives me 500 Internal Error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.