开发者

How to use mod_rewrite to remove a query string withing a CMS

开发者 https://www.devze.com 2023-04-09 03:23 出处:网络
I\'m using a CMS that sends all requests to an index.php file using the following RewriteRule Rewrite开发者_运维问答Rule .* index.php [L]

I'm using a CMS that sends all requests to an index.php file using the following RewriteRule

Rewrite开发者_运维问答Rule .* index.php [L]         

However in the news section of the site the CMS is generating news links like this: /news?month=201106

I want my news links like this: /news/month/201106 and I will achieve this with PHP code.

I know pretty much how to achieve the rewrite with Apache if it weren't for that catchall I would use something like this:

RewriteRule ^news/month/(.+)$ news?month=$1 

However my problem is that the CMS is catching the calls and trying to find /news/month/201106 which it cannot and throws an CMS level 404

I've read up about making exceptions but I can't work out how to get:

  1. Apache to catch the rewrite before it gets sent to the catch all
  2. The CMS to then process the rewritten URL as normal (ie: receive news?month=201106 and process that as normal)

I'm sure this is probably down to the Rewrite flag and the order in which these directives are written but I just cannot get it to work.


1) Apache to catch the rewrite before it gets sent to the catch all

You can do this by adding a RewriteCond before your RewriteRule .* index.php [L], so that it looks something like this:

RewriteCond %{REQUEST_URI} !^/news
RewriteRule .* index.php [L]

2) The CMS to then process the rewritten URL as normal (ie: receive news?month=201106 and process that as normal)

The 2nd rule that you had, RewriteRule ^news/month/(.+)$ news?month=$1 should take care of that.

0

精彩评论

暂无评论...
验证码 换一张
取 消