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条)

报告相同问题?

悬赏问题

  • ¥20 5037端口被adb自己占了
  • ¥15 Error in check.length("fill") : 'gpar'成分'fill'的长度不能为零
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误