Rewrite URL from pretty to ugly? /something -> ?a=somthing [duplicate]

 ̄綄美尐妖づ 提交于 2020-01-15 08:31:09

问题


I'm looking to use url rewrite to access this...

http://url.com/?a=value

via...

http://url.com/value

So that by using url.com/value I'm actually loading url.com/?a=value.

I'm assuming I need to put a rewrite in an htaccess file and then somehow detect this in index.php and pull out the field value? It's a bit beyond me unfortunately so would really appreciate your help.

Thanks!


回答1:


This is a standard htaccess file for "pretty" urls like the one you need:

#.htaccess file
Options +FollowSymLinks
RewriteEngine On
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?a=$1 [L]

then in your index.php you can get the initially requested path from the 'a' variable.

$path = $_GET['a']; 



回答2:


This .htaccess code takes into consideration something like http://url.com/value?parameter=something

RewriteEngine On

RewriteRule ^([A-Za-z0-9-/]+)\&(.*)$ /$1?$2 [R]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-/_]+)$ /index.php?a=$1&${QUERY_STRING} [L,QSA]


来源:https://stackoverflow.com/questions/8101063/rewrite-url-from-pretty-to-ugly-something-a-somthing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!