简体   繁体   中英

How make this htaccess redirect correctly

I want to redirect this urls using htaccess RewriteRule :

news/tags/video to news.php?t=video
news/tags/interview to news.php?t=interview
news/tags/news to news.php?t=news 

the urls: news.php?t=video, news.php?t=interview, news.php?t=news are working fine and I recive the "t" var correctly. But when I apply htaccess RewriteRules the "t" var is not passed.

This is my code:

<IfModule mod_rewrite.c>
    #this rules are working fine
    RewriteRule ^news[/]?$ news.php
    RewriteRule ^news/(.+)[/]?$ news_article.php?idN=$1

    #problem rules
    RewriteRule ^news/tags/video[/]?$ news.php?t=video
    RewriteRule ^news/tags/interview[/]?$ news.php?t=interview
    RewriteRule ^news/tags/news[/]?$ news.php?t=news
</IfModule>

The pattern ^news/(.+)[/]?$ matches news/tags/video (etc) and rewrites the URL to news_article.php and no further matching is possible. You need to tweak this rule just a little:

RewriteRule ^news/?$ news.php
RewriteRule ^news/([^/]+)/?$ news_article.php?idN=$1
RewriteRule ^news/tags/video/?$ news.php?t=video
RewriteRule ^news/tags/interview/?$ news.php?t=interview
RewriteRule ^news/tags/news/?$ news.php?t=news

Rewrites are handled in order of matching. If other rules exist that return true first your redirect states will likely not be matched. In your case a match was being found prior to reaching your additional rules.

<IfModule mod_rewrite.c>
  RewriteRule ^news/tags/([^/]+)/?$ news.php?t=$1 [L]
  RewriteRule ^news[/]?$ news.php [L]
  RewriteRule ^news/([^/]+)[/]?$ news_article.php?idN=$1 [L]
</IfModule>

Please change this RewriteRule directive on your code:

RewriteRule ^news/(.+)[/]?$ news_article.php?idN=$1

... to this one:

RewriteRule ^news/([^/]+)[/]?$ /news_article.php?idN=$1

Because the previous rule matches everything, including "/" slash. And just use this single rule below as the solution on your #problem rules :

RewriteRule ^([^/]+)/tags/([^/]+)[/]?$ /$1.php?t=$2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM