Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL rewriting without changing URL .htaccess

I want to rewrite my URL

From:

https://example.com/fr/transporter/transporterPublicProfile.php?profil=1927

To:

https://example.com/fr/profil-des-transporteurs/1927

When ever a user visit this URL:

https://example.com/fr/transporter/transporterPublicProfile.php?profil=1927

It should appear like this:

https://example.com/fr/profil-des-transporteurs/1927

And if a user visit this URL:

https://example.com/fr/profil-des-transporteurs/1927

Its should remain as it is.

What I have tried so far is:

RewriteRule ^profil-des-transporteurs/(.*)$ /fr/transporter/profil.php?oid=$1 [L]

It changes the URL as I want but the problem is that, the links on that page doesn't works properly. The page is using relative paths for images etc w.r.t new URL i.e:

https://example.com/fr/profil-des-transporteurs/
like image 509
StormTrooper Avatar asked Dec 19 '14 14:12

StormTrooper


1 Answers

You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ fr/profil-des-transporteurs/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^fr/profil-des-transporteurs/([^/.]+)/?$ fr/transporter/transporterPublicProfile.php?profile=$1 [L,QSA,NC]

Then for resolving relative links used for images/js/css paths you can add this in the <head> section of your page's HTML:

<base href="/fr/transporter/" />
like image 190
anubhava Avatar answered Oct 11 '22 11:10

anubhava