2

I am working on a site and learning PHP at the same time so it is relatively new to me. I am working on URL rewrite currently, however, I am not getting the desired result.

I am trying to change: example.com/CMS/listing?id=1 into example.com/CMS/listing/1.

Using the .htaccess file held in the root of the files, I have the following:

RewriteEngine On
RewriteBase /
RewriteRule CMS/listing/([0-9]+) CMS/listing.php?id=$1

And on the site, I have a simple echo function which gets the query string param for id. When I navigate to example.com/CMS/listing?id=1 the function displays 1, however, when I go to example.com/CMS/listing/1 it does not show anything. Is there something I'm missing or doing wrong?

# Get QString Param
$id = $_GET["id"];
echo $id;

Footnote

.htaccess is held at the root, and listing.php is within a folder called CMS.

0

2 Answers 2

2

It looks like you have a conflict with MultiViews. You should disable MultiViews at the top of your .htaccess file:

Options -MultiViews

If MultiViews is enabled then when you request /CMS/listing/1, mod_negotiation will map the request to /CMS/listing.php (appending the file extension, without any URL parameter) before your mod_rewrite directive is able to process the request. (/1 is seen as additional pathname information or PATH_INFO on the URL.)

A URL like /CMS/listing?id=1 (without the file extension) will only work if MultiViews is enabled. In this case, the query string is already present on the URL.

Sign up to request clarification or add additional context in comments.

Comments

0

You could also redirect all traffic through to index.php and handle it all in PHP. That is how other CMS' and framework usually do it.

This is the Laravel approach - you don't have to do it this way, but later on when you want to do custom urls on news post or any other this, you have to setup new rules. This way you can just add them to the database and create some kind off route handler. If you want to keeps it simple you could then just create a table in the database where you store urls and a field for the file to be called.

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.