Rewriting URLs without Changing .htaccess URLs

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 a user visits this URL:

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

It should look like this:

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

And if the user visits this URL:

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

He must remain as he is.

I have tried so far:

 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 this page are not working properly. The page uses relative paths for images, etc. Wrt new url ie:

 https://example.com/fr/profil-des-transporteurs/ 
+5
source share
2 answers

This code can be used in the 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, to resolve the relative links used for the images / js / css paths, you can add this to the <head> section of the HTML page:

 <base href="/fr/transporter/" /> 
+3
source

You can use the base tag in your html so that all paths will refer to it

 <base href="https://example.com/fr/transporter/"> 
+2
source

Source: https://habr.com/ru/post/1209552/


All Articles