1

I want a nice looking url that can change https://www.url.com/profile.php/?lang=en&user=1#userinfo into https://url.com/profile/en/1#userinfo. And the lang variable is on every page.

I tried to get my extension away and it works

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

But now i need the get variables, i have looked around but nothing works.

Peter Arents
  • 27
  • 1
  • 12

1 Answers1

1

These rules check the request line (e.g., "GET /index.html HTTP/1.1"), the request path is /profile.php, and if contains the query string lang=&user=, redirect the request uri from /profile.php to /profile/%1/%2, %1, %2 is the back-reference to match the grouped part ([^&]+)

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /profile\.php
RewriteCond %{QUERY_STRING} lang=([^&]+)&user=([^&]+)
RewriteRule ^/?profile.php$ /profile/%1/%2 [NE,R,L,QSA]

This rule internal rewrite the URL from /profile/{lang}/{user_id} to /profile.php?lang={lang}&user_id={user_id}. So when uri /profile/en/1 is requested, the server knows the rewritten uri is /profile.php?lang=en&user_id=1.

RewriteRule ^/?profile/([^/]+)/([^/]+)$ /profile.php?lang=$1&user_id=$2 [L]
Ben
  • 5,069
  • 4
  • 18
  • 26