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 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助