简体   繁体   中英

How to remove index.php and hide parameter lang from url?

I have the following URL

http://localhost/apps/site/index.php?lang=es

I want to make it like the following using .htaccess

http://localhost/apps/site/es

.htaccess rules

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)/?$ /index.php?lang=$1 [L,QSA]

</IfModule>

When I try this url http://localhost/apps/site/es directly redirects me to http://127.0.0.1/dashboard/

I have tried this solution but not works for me:

https://stackoverflow.com/a/30153794/7705186

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/apps/site/index.php$
RewriteCond %{QUERY_STRING} ^lang=(\w+)
RewriteRule apps/site/index.php /apps/site/%1 [QSD,L]
  • First condition is to tell the module to apply the rule only if the request URI exactly /apps/site/index.php

  • second condition if the first query parameter name is lang and it's value is a word. The () is to capture the value of the parameter and save it in %1

  • In the rule we tell the module to remove apps/site/index.php and rewrite it by /apps/site/%1 while replacing %1 with the value we captured from the second condition

  • QSD Query String Discard flag is to make the module not append the query to the final url (you must set it before L flag , this will not work [L,QSD] )

Now if you requested

http://localhost/apps/site/index.php?lang=es

it will be rewritten to

http://localhost/apps/site/es

you can test and play with rewrite rules on this site https://htaccess.madewithlove.be/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM