duanqiao1949 2015-06-08 12:55
浏览 297
已采纳

从/index.php?page=about重写网址到/ page /

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

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

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

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);

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.

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

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?

  • 写回答

3条回答 默认 最新

  • drpsrvu85668 2015-06-08 13:09
    关注

    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:

    • ^ 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
      • ] ends the character class
      • + specifies that the character class should match one or more times
    • ) ends the submatch group
    • $ anchors the match at the end of the string

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?