【问题标题】:Rewrite URL from pretty to ugly? /something -> ?a=somthing [duplicate]将 URL 从漂亮改成丑陋? /something -> ?a=somthing [重复]
【发布时间】:2011-12-27 09:57:09
【问题描述】:
【问题讨论】:
标签:
php
apache
url
mod-rewrite
【解决方案1】:
这是一个标准的 htaccess 文件,用于“漂亮”的 url,就像你需要的那样:
#.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]
然后在您的 index.php 中,您可以从“a”变量中获取最初请求的路径。
$path = $_GET['a'];
【解决方案2】:
这个 .htaccess 代码考虑了 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]