3

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.

4

2 回答 2

1

尝试将这些规则添加到文档根目录中的 htaccess 文件中:

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]

第一个重定向请求,例如/index.php?view=news&task=footo /news/foo/。然后第二条规则在内部将请求重写/news/foo//index.php?view=news&task=foo.

如果您的所有链接都看起来像/news/foo/,那么您将不需要第一条规则。

于 2013-10-07T22:12:47.277 回答
0

我不是 htaccess 的老手,但这样的事情可能会奏效:

RewriteRule /news/(.*) /index.php?view=news&task=$1 [L]

这应该将对 /news/abc 的所有请求重写为 index.php?task=news&view=abc。反向引用$1代表 RegEx 中被括号包围的第一个模式。

于 2013-10-07T22:09:04.533 回答