Munib
Munib

Reputation: 3661

How to Rewrite a URL with an Id and text using htaccess

Actually I don't know the regex etc and I want to rewrite URL using .htacess. I have the Following URL

http://www.example.com/post.php?post_id=114&post_txt=this-should-not-happen-again

I want to rewrite it like this

http://www.example.com/114/this-should-not-happen-again

What Should I write in .htaccess file to achieve my target?

Upvotes: 0

Views: 289

Answers (3)

goodeye
goodeye

Reputation: 2449

Give this a try: It looks for digits, slash, any-characters. It inserts the digits in the $1, and the any-characters in $2.

RewriteEngine On

RewriteRule ^(\d+)/(.+)$ /post.php?post_id=$1&post_txt=$2 [NC,L]

Upvotes: 1

Eli
Eli

Reputation: 38899

RewriteEngine on
RewriteRule ^/post.php?post_id=(\d+)&post_txt=(.*)$ http://www.example.com/$1/$2 [R=301,L]

Also, a very similar question exists on superuser here

Upvotes: 0

anubhava
anubhava

Reputation: 784898

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /post.php?post_id=$1&post_txt=$2 [L,QSA]

Upvotes: 0

Related Questions