doulian1852 2014-03-23 02:54
浏览 31
已采纳

使用.htaccess重写网址

I am trying to convert urls like this http://example.com/page.php?user=x&post=x into http://example.com/blogs/user/post

This is the code I have so far, not sure if I'm missing something or have done something wrong as it haven't been successful so far.

RewriteEngine On
RewriteRule ^blogs/([a-z0-9\-]+)/([0-9]{1,11})/$ page.php?user=$1&post=$2 [L]

EDIT: After making some amendments, it returns now an error 404 error saying page.php is not found. I know for certain the file is there.

2ND EDIT: Resolved 404 issue.

The post parameter is optional as well.
user can will have a mix of A-z0-9 (no character limit)
post can only be 0-9 with and upto 11 characters in length

  • 写回答

3条回答 默认 最新

  • dqxm14187 2014-03-23 03:41
    关注

    Try

    RewriteRule ^blogs/?([a-z0-9]*)?/?$ page.php?user=$1&post=$2 [NC,L]
    

    The ? indicates that the matching is optional. Therefore for /? the slash is optional.

    And for the second section after the ^blogs/?

     ([a-z0-9]*)?
    

    If you remove the ? and * from the above to become like this :

    RewriteRule ^blogs/?([a-z0-9])/?$ page.php?user=$1&post=$2 [NC,L]
    

    The user will no longer become optional and must be fully matched. Example http://blogs/John; If you put + in place of * like below:

    RewriteRule ^blogs/?([a-z0-9]+)/?$ page.php?user=$1&post=$2 [NC,L]
    

    The user need to be matched with at least 1 character. For example : http://blogs/J

    And if you put * asterisk like below:

    RewriteRule ^blogs/?([a-z0-9]*)/?$ page.php?user=$1&post=$2 [NC,L]
    

    It can match to zero length. For example : http://blogs/ . Almost like optional.

    If the match failed you will get the 404 Not found error

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据