使用PHP& amp; htaccess漂亮的URL URL变量 [英] htaccess pretty URL with PHP & URL variables

查看:72
本文介绍了使用PHP& amp; htaccess漂亮的URL URL变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以在这里提供一些帮助。这是多层的问题。简而言之,我想拥有漂亮的URL,这些URL使用URL变量来访问文件夹中的文件。

hoping someone can offer some assistance here. This is an issue with multiple layers. In short, I want to have pretty URLs that use a URL variable to a file within a folder.

所以,我要 http ://www.example.com/?page = path / to / page 看起来像 http://www.example.com/path/to/page/

在index.php页面上,加载上述内容:

On the index.php page, to load the above content:

require_once('content/'.$_GET['page'].'.php');

当前htaccess配置:

Current htaccess configuration:

RewriteEngine On

# Add trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]

# Make URLs sexy!
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

我可以使它与/ content文件夹中的顶级页面一起使用,但是,当我要包含的php文件位于/ content文件夹中的文件夹中时,它将无法工作。

I can get this to work with a top level page within the /content folder, however, when the php file I with to include is within a folder inside the /content folder, it does not work.

任何想法??我认为问题归结为在?page =变量中使用加号/。 htaccess似乎不喜欢这样。

Any ideas?? I believe the issue comes down to using an addition / in the ?page= variable. htaccess doesnt seem to like that.

推荐答案

经过一番游戏,我弄清楚了。我的正则表达式已关闭。

After some playing, I figured it out. My regex was off.

已更改:

# Make URLs sexy!
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

至:

RewriteRule ^(.*)(/)/?$ index.php?page=$1 [L]

这篇关于使用PHP& amp; htaccess漂亮的URL URL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆