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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用