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

从/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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀