Pretty URLs .htaccess rewrite engine - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Sunday, October 1, 2023

Pretty URLs .htaccess rewrite engine

I'd like to combine two rules together so I could have pretty URLs

both of index - mysite.com/en => mysite.com/?lang=en as well as further directories - mysite.com/en/blog/trip => mysite.com/blog/trip?lang=en

my .htaccess file looks like this, I tried to use the code from here but that works just for the second part of my problem

# Default index
DirectoryIndex index.php

RewriteEngine On

# Rewrite "/<lang>/<directory>/" to `/<directory>/index.php?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2/index.php -f
RewriteRule ^([a-z]{2})/(.*?)/?$ $2/index.php?lang=$1 [L]

# Rewrite "/<lang>/<directory>/<file>" to `/<directory>/<file>?lang=<lang>"
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([a-z]{2})/(.+) $2?lang=$1 [L]

# add .php extension 
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Thank you for your time!



source https://stackoverflow.com/questions/77208109/pretty-urls-htaccess-rewrite-engine

No comments:

Post a Comment