0

I've looked around, and most things either seem overkill, or I'm just not understanding something.

I've got the following URLs:

http://cms.dev/article.php?post_id=6

And I like to replace them with

http://cms.dev/article/6/

I've got the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

However, this just cleans URLs like /admin.php to /admin.

What is the best way to clean a URL, but keep the .php removed from other pages?

2 Answers2

0

You can use:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/([0-9]+)/?$ /$1.php?post_id=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
anubhava
  • 664,788
  • 59
  • 469
  • 547
0

Simply,

RewriteEngine on
RewriteRule ^article/([0-9]+)$ article.php?post_id=$1
RewriteRule ^article/([0-9]+)/$ article.php?post_id=$1

Source: 10 Simple examples to rewrite using htaccess

LIGHT
  • 5,190
  • 10
  • 33
  • 75
  • Thanks. How come the rewrite rule is written twice? –  Apr 14 '14 at 14:19
  • 1
    For both `http://cms.dev/article/6/` and `http://cms.dev/article/6`. i.e. with or without backslash at end – LIGHT Apr 14 '14 at 14:20
  • 2
    You can do `^article/([0-9]+)/?$ article.php?post_id=$1` to condense it to a single line. The "?" means that the last "/" is optional. – GarethL Apr 14 '14 at 14:26