user2737402
user2737402

Reputation:

htaccess redirect with unique id to title

I have URL's like this:

http://www.example.com/en/product.php?id=23&t=page-title-here

I want change URL's to something like this :

http://www.example.com/en/product23/page-title-here/

Upvotes: 0

Views: 511

Answers (1)

Mike Rockétt
Mike Rockétt

Reputation: 9007

Place the following in your /.htaccess file:

RewriteEngine on

# Step 1: Redirect file-based URIs to new 'pretty permalinks' and prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=([^/]+)$ [NC]
RewriteRule ^(en/product).php$ /$1%1/%2? [R=302,NE,L]

# Step 2: Rewrite above permalink to file-based URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en/product)(\d+)/([^/]+)/?$ /$1.php?id=$2&t=$3 [L,QSA]

Update: If you want to match multiple languages (per your comment below), you can use a non-capture group that checks for exactly two characters instead of just en:

# Use this rule in step 1 above
RewriteRule ^((?:[a-z]{2})/product).php$ /$1%1/%2? [R=302,NE,L]
# Use this rule in step 2 above
RewriteRule ^((?:[a-z]{2})/product)(\d+)/([^/]+)/?$ /$1.php?id=$2&t=$3 [L,QSA]

Upvotes: 1

Related Questions