Search code examples
php.htaccess

htaccess rewrited url and php GET method


How to manage the GET method if I have changed the URLs?

I changed the PHP webpages URL from

website.com/article.php?id=1&name=NewStory

to

website.com/article/1/NewStory

.htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&name=$2 [NC,L]

Before, building the URL was easy because you add to the URL your info, like:

article.php?id=1&name=NewStory

Taking the info from URL using GET method was also easy, like:

$articleID = $_GET['id'];
$articleName = $_GET['name'];

Could you please help me with a solution for keeping the URLs as I have changed with the .htaccess file and manage the GET method?

How to take the info from a URL like this, using the GET method?

website.com/article/1/NewStory

How to write a URL to keep it as per the .htaccess file?

Thank you!


Solution

  • This is because your new URL /article/1/NewStory is currently pointing to the /article.php file via Multiviews and not via the RewriteRule

    To fix this , You need to disable Multiviews and remove the RewriteCond %{REQUEST_FILENAME}\.php -f condition as its checking for an existent PHP file to rewrite.

    Options -Multiviews
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&name=$2 [NC,L]