L.Lawliet
L.Lawliet

Reputation: 113

Php url rewrite and redirect in htacess

I want to redirect my site to friendly. I have my website pull data from database and adress is /post?id=1 and i want to change it to /post/1 I already wrote the code for rewrite but i cant make sense from google research how to redirect to /post/1.

my code

Options +FollowSymLinks
 RewriteEngine on
 RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L]

Upvotes: 1

Views: 55

Answers (2)

Kalidass
Kalidass

Reputation: 434

This one worked for me

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ post.php?id=$1  [NC]

if you goto url /post/check1/

the htaccess file internally call post?id=check1

Upvotes: 0

Croises
Croises

Reputation: 18671

Use:

Options +FollowSymLinks
RewriteEngine on

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /post/%1? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L,QSA,NC]

Upvotes: 2

Related Questions