重写规则的作品,但丑陋的URL显示,而不是pretty的1 [英] RewriteRule works but ugly URL shows instead of pretty one
问题描述
这是我的.htaccess文件:
This is my .htaccess file:
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
所以我改写了我的链接,让他们看起来像这样:
So I rewrote my links, so that they look like this:
<a href="/admin/menu/1/1/bl4h1234">Some link</a>
和工作正常,但在地址栏中的URL丑陋的URL显示,但整个目的是要显示的URL为pretty的URL:/
And that works fine, but the URL shows in the address bar as the ugly URL, but the whole purpose was to show the URL as the pretty URL :/
我该如何解决呢?
推荐答案
这是另一种的(安培;标准[MVC /前端控制器模式])的方式来处理mod_rewrite规则和改写是通过全网址为您的index.php,然后再处理它。
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.
,因为它似乎你正在使用的文件夹(菜单|管理员)
与的index.php 的每个,你有没有那种路由器脚本。
所以,你将需要处理的的.htaccess
的基本路线。你基本上只需要一个重写的每个文件夹。
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.
本的.htaccess去在你的根。否则,你将需要重写为每个文件夹和不使用的RewriteBase /路径
The .htaccess goes in your root. Else you would need a rewrite for each folder and without the RewriteBase /path
目录结构(这里放的.htaccess,在根目录):
ROOT>/
/index.php
/.htaccess
/admin/
/index.php
/menu/
/index.php
/someOtherFolder/
/index.php
/somefile.php
的的.htaccess重写
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]
那么你的index.php文件中,你处理的路线,通过爆炸的 $ _ GET ['路线']
参数由 /
<?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.
这篇关于重写规则的作品,但丑陋的URL显示,而不是pretty的1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!