1

这是我的 .htaccess 文件:

RewriteEngine on

RewriteBase /admin

RewriteRule menu/([0-9]+)/([0-9]+)/([a-z0-9]+) http://www.mysite.com/admin/index.php?m=$1&o=$2&token=$3 [R,L]

我必须包含完整的 URL,因为没有它,它会一直重定向到http://www.mysite.com/menu/1/1/login.php而不是mysite.com/admin/login.php

所以我重写了我的链接,使它们看起来像这样:

<a href="/admin/menu/1/1/bl4h1234">Some link</a>

这很好用,但是 URL 在地址栏中显示为丑陋的 URL,但整个目的是将 URL 显示为漂亮的 URL:/

我该如何解决?

4

3 回答 3

1

An alternative (& standard [MVC / Front controller Patterns]) way to handle mod_rewrite rules and rewriting is to pass the whole url to your index.php and then process it there.

It actually makes it simpler in the long run, otherwise the complexity of your problems will only increase as you add more features.

As it seems you are using folders (menu|admin) with an index.php in each, you have no kind of router script. So you will need to handle the basic route in the .htaccess. You basically just need a rewrite for each folder.

The .htaccess goes in your root. Else you would need a rewrite for each folder and without the RewriteBase /path

Directory Structure (Where to put the .htaccess, in root):

ROOT>/
     /index.php
     /.htaccess
     /admin/
           /index.php
     /menu/
          /index.php
     /someOtherFolder/
                     /index.php
                     /somefile.php

The .htaccess rewrite

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^admin/menu/(.*)$ admin/index.php?route=$1 [L,QSA]
RewriteRule ^menu/(.*)$ index.php?route=$1 [L,QSA]

Then within your index.php files you handle the route, by exploding the $_GET['route'] param by /

<?php 
if(isset($_GET['route'])){
    $url = explode('/',$_GET['route']);
    //Assign your variables, or whatever you name them
    $m     = $url[0];
    $o     = $url[1];
    $token = $url[2];
}else{
    $m     = null;
    $o     = null;
    $token = null;
}
?>

Hope it helps.

于 2012-04-29T15:47:47.637 回答
1

您正在通过 重定向到新 URL [R]。相反,从重写中删除协议和域并丢失[R]. 这将执行内部重写。

RewriteRule menu/([0-9]+)/([0-9]+)/([a-z0-9]+) index.php?m=$1&o=$2&token=$3 [L]
于 2012-04-29T12:41:25.233 回答
0
  1. 不要创建对绝对 URL 的重写。从重写的 URL 中删除协议和主机名。
  2. 如果您不想重定向,请不要使用 R 标志。
于 2012-04-29T12:41:22.280 回答