douqiang3768 2014-01-02 19:10
浏览 49
已采纳

htaccess重写后在URL中GET变量

I'm trying to create a .htaccess file that needs to see the directories as 1 variable: ex. http://example.com/this-is-page-one/sub-dir will pass this-is-page-one/sub-dir as a variable to PHP.

I have this at the moment:

Options +FollowSymLinks
RewriteEngine on

#  Page rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /page.php?url=$1

#  Add trailing slash to urls
RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L]

This works fine as long as you type a trailing slash behind the URL. But as you can see the htaccess will put a trailing slash behind the URL for you. When that happens, it automatically puts the ?url= variable behind it.

ec. http://example.com/this-is-page-one/sub-dir/?url=this-is-page-one/sub-dir

Does someone see what I do wrong?

  • 写回答

1条回答 默认 最新

  • douzangdang2225 2014-01-02 19:15
    关注

    Move the second set of condition and rule before the first:

    Options +FollowSymLinks
    RewriteEngine on
    
    #  Add trailing slash to urls
    RewriteCond %{REQUEST_URI} !(/$|\.) 
    RewriteRule ^(.*)$ %{REQUEST_URI}/ [R=301,L]
    
    #  Page rewriting
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /page.php?url=$1
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?