dougua2309 2013-10-07 22:00
浏览 37
已采纳

如何使用htaccess来做到这一点?

I got stuck at this point here, and I'm breaking my head trying to figure out why its not working!

This is want to get the url to look like

 example.com/news/top/

from

 example.com/index.php?view=news&task=top

but i need it that way that "task" can be something diffrent like

 example.com/index.php?view=news&task=newest
 example.com/index.php?view=news&task=help
 example.com/index.php?view=news&task=write
 ....

current .htaccess looks like:

DirectoryIndex index.php
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2

If I visit www.example.com/news/ -> it only loads the index.php but does not pass ?view=news

If I visit www.example.com/news/top/ -> it works just fine.

But I need them both to work, adding a new line to .htaccess like :

RewriteRule ^news/ index.php?view=news

then visiting www.example.com/news/ works fine, but visiting www.example.com/news/top/ it will not be possible to get the value of task.

Please help me!

Thank you in advance.

  • 写回答

2条回答 默认 最新

  • doumiebiao6827 2013-10-07 22:12
    关注

    Try adding these rules to the htaccess file in your document root:

    RewriteEngine On
    
    # Redirect access to index.php
    RewriteCond %{THE_REQUEST} \ /index\.php\?view=([^&]+)&task=([^&\ ]+)
    RewriteRule ^ /%1/%2/? [L,R=301]
    
    # rewrite to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?view=$1&task=$2 [L]
    

    The first redirects requests like /index.php?view=news&task=foo to /news/foo/. Then the second rule internally rewrites requests like /news/foo/ back to /index.php?view=news&task=foo.

    If all of your links already look like /news/foo/, then you won't need the first rule.

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路