dshdb64088 2009-07-12 01:24
浏览 11
已采纳

.htaccess和$ _GET重写

I am having an annoying issue. I have a script which uses the $_GET feature of php to get a variable from the url of the page. However, I need to know how to, first and foremost, make my page url go from

http://www.example.com/dir/dir2/page.php?value=something&anothervalue=somethingelse

to

http://www.example.com/something/dir2/somethingelse/page.php

This is confusing for me.

Now, here is the tough part: How do I $_GET the values of value and anothervalue while leaving any other url values intact at the end of page.php? Also, if a user visits http://www.example.com/something/dir2/somethingelse/page.php they will be shown the content for http://www.example.com/dir/dir2/page.php?value=something&anothervalue=somethingelse , and vice versa, right?

This is very important to the operation of my site, thanks for helping in advance! I don't know anything about .htaccess at all but I do know that it is possible to do this.

To clarify, doing this very confusing thing will actually make things much simpler in the long run as it will require much less recoding and will provide cleaner URLS.

  • 写回答

3条回答 默认 最新

  • dqce48404 2009-07-12 04:59
    关注

    This rule should do it:

    RewriteCond %{REQUEST_URI} !^/dir/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/(.*) dir/$2/$4?value=$1&anothervalue=$3 [L,QSA]
    

    It will rewrite requests of /something/dir2/somethingelse/page.php internally to /dir/dir2/page.php?value=something&anothervalue=somethingelse.

    But that wouldn’t prevent from requesting the latter directly but just allowing an alias.

    Redirecting requests of /dir/dir2/page.php?value=something&anothervalue=somethingelse externally to /something/dir2/somethingelse/page.php is also possible with mod_rewrite but requires a little more complex rule since the URL parameters can have an arbitrary order:

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{QUERY_STRING} ^(([^&]*&)*)value=([^&]+)&?([^&].*)?$
    RewriteCond %3&%1%4 ^([^&]+)&(([^&]*&)*)anothervalue=([^&]+)&?([^&].*)?$
    RewriteRule ^dir/([^/]+)/(.*) /%1/$1/%4/$2?%2%5 [L,R=301]
    

    As you see, that’s not really nice. A better solution would be to check with PHP what URL has been requested (see $_SERVER['REQUEST_URI']) and do the redirect with PHP (see header function).

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

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算