【问题标题】:Rewrite URL from pretty to ugly? /something -> ?a=somthing [duplicate]将 URL 从漂亮改成丑陋? /something -> ?a=somthing [重复]
【发布时间】:2011-12-27 09:57:09
【问题描述】:

我正在寻找使用 url 重写来访问这个...

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

通过...

http://url.com/value

因此,通过使用 url.com/value,我实际上是在加载 url.com/?a=value。

我假设我需要在 htaccess 文件中进行重写,然后以某种方式在 index.php 中检测到这一点并提取字段值?不幸的是,这超出了我的范围,因此非常感谢您的帮助。

谢谢!

【问题讨论】:

标签: 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]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-15
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多