将网址从/index.php?page=about重写为/ page / [英] rewrite URL from /index.php?page=about to /page/

查看:179
本文介绍了将网址从/index.php?page=about重写为/ page /的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定此消息已发布在其他地方,但找不到。我花了几个小时研究和尝试从示例代码到生成器的一切。我有一个站点当前位于名为 new的子文件夹中。准备好将其启动后,我会将网站移至根文件夹。该站点是数据库驱动的,现在访问您需要使用以下网址的页面:

I'm sure this has been posted somewhere else but I can't find it. I have spent a few hours researching and trying everything I can thing of from sample code to generators. I have a site that is currently residing in a subfolder called "new". I will be moving the site to the root folder once it is ready for launch. The site is database driven and right now to access the pages you need to use a url like this:

http://domain.com/new/index.php?page=about

我想要它,因此URL看起来像这样:

I would like to have it so the URL looks like this:

http://domain.com/new/about/

我尝试了很多不知道从哪里开始的事情。这是我使用的index.php中的PHP代码:

I have tried so many different things I don't know where to start. Here is the PHP code in index.php I use:

$page = (isset($_GET['page']) ? $_GET['page'] : 'home');
$data = get_data($page);

get_data()只是获取内容根据URL中传递给它的变量从数据库中删除。是的,在查询数据库之前,函数中的所有内容均已正确转义。

The get_data() simply grabs the content from the database based on the variable that was passed to it in the URL. Yes, everything is properly escaped in the function before querying the database.

这是我当前的.htaccess文件:

Here is my current .htaccess file:

Options +FollowSymLinks
RewriteEngine On

RewriteBase /new/

RewriteCond %{REQUEST_FILENAME} -f [NC,OR] 
RewriteCond %{REQUEST_FILENAME} -d [NC] 
RewriteRule .* - [L]

RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1

/ new / home /可以正常工作,但/ new / contact-us /等其他任何设备都不能工作。我唯一能想到的是/ home /是网址中唯一没有'-'字符的一个。我是否在我的.htaccess中缺少某些帐户来解决这个问题?

Oddly enough /new/home/ works but any other like /new/contact-us/ does not work. The only thing I can think of is /home/ is the only one without a '-' character in the url. Am I missing something in my .htaccess to account for this?

推荐答案


奇怪的是/ new / home /可以使用,但其他任何类似/ new / contact-us /都不能使用。我唯一能想到的是/ home /是网址中唯一没有'-'字符的一个。我是否在我的.htaccess中缺少某些帐户来解决这个问题?

Oddly enough /new/home/ works but any other like /new/contact-us/ does not work. The only thing I can think of is /home/ is the only one without a '-' character in the url. Am I missing something in my .htaccess to account for this?

您绝对会丢失一些用于弥补破折号的问题。您正在使用的正则表达式-

You are absolutely missing something to account for the dash. The regular expression you are using --

^([a-zA-Z0-9]+)$

使用不包含破折号的字符范围。让我们按部分分解正则表达式:

Uses a character range that does not include the dash. Let's break down the regular expression by part:


  • ^ 将匹配项锚定到字符串的开头

  • 开始子匹配分组

    • [开始一个字符类-这个是 [a-zA-Z0-9] + ,它匹配三个不同的范围或多次;指定的范围为:

      • az 匹配小写字母字符 abcdefghijklmnopqrstuvwxyz

      • AZ 匹配大写字母字符 ABCDEFGHIJKLMNOPQRSTUVWXYZ

      • 0-9 匹配数字字符 0123456789

      • ^ anchors the match to the beginning of a string
      • ( begins a submatch grouping
        • [ begins a character class -- this one is [a-zA-Z0-9]+, which matches three distinct ranges one or more times; the ranges specified are:
          • a-z matches lowercase alphabetic characters abcdefghijklmnopqrstuvwxyz
          • A-Z matches uppercase alphabetic characters ABCDEFGHIJKLMNOPQRSTUVWXYZ
          • 0-9 matches numeric characters 0123456789

          请注意,您在角色类中指定的范围都不包含破折号。为了同时匹配破折号,您需要在字符类中包括破折号,如下所示:

          Note that none of the ranges you've specified in your character class include the dash. To also match the dash, you need to include the dash in the character class, like so:

          ^([a-zA-Z0-9-]+)$
          

          有关字符类的更多信息,请在正则表达式有很多信息。

          For more information on character classes, the entry on regular expressions in Wikipedia has a good bit of information.

          这篇关于将网址从/index.php?page=about重写为/ page /的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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