tags:

views:

63

answers:

4

I am trying to use http://www.example.com/news/id/21/title/top-10-things/?page=1 for sending the page parameter and it is not working in php

below is my setting in the .htaccess file

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2
RewriteRule ^news/$ /news.php
+3  A: 

Try appending the %{QUERY_STRING} to the URLs in your htaccess.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4&%{QUERY_STRING}
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2&%{QUERY_STRING}
RewriteRule ^news/$ /news.php&%{QUERY_STRING}

As Daniel noted, you can also use the mod_rewrite qsappend or QSA flag for this purpose. REF: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4 [QSA]
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2 [QSA]
RewriteRule ^news/$ /news.php [QSA]
BBonifield
You can use the [QSA] (query string append) flag instead.
Daniel Vandersluis
+1  A: 

Set the QSA flag to get the requested query automatically appended to the one in substitution URL:

RewriteRule ^news/(.*)/(.*)/(.*)/(.*)/$ /news.php?$1=$2&$3=$4 [QSA]
RewriteRule ^news/(.*)/(.*)/$ /news.php?$1=$2 [QSA]
RewriteRule ^news/$ /news.php [QSA]

Furthermore, you shouldn’t use .* if you can be more specific. In this case using [^/]+ instead avoids unnecessary backtracking:

RewriteRule ^news/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /news.php?$1=$2&$3=$4 [QSA]
RewriteRule ^news/([^/]+)/([^/]+)/$ /news.php?$1=$2 [QSA]
RewriteRule ^news/$ /news.php [QSA]

And for a general solution for an arbitrary number or parameters, see Rewriting an arbitrary number of path segments to query parameters.

Gumbo
A: 

try changing the regular expressions to
RewriteRule ^news/([^/])/([^/])/([^/])/([^/])/?page=(.)$ /news.php?$1=$2&$3=$4&page=$5
RewriteRule ^news/([^/].
)/([^/].)/?page=(.)$ /news.php?$1=$2&page=$3
RewriteRule ^news/$ /news.php

aeon
A: 

I have had problems with this as well. I usealy make a dedicated .htaccess rewrite for the page. so, RewriteRule ^news/([^/])/([^/])/([^/])/([^/])/?page=(.)$ /news.php?$1=$2&$3=$4&page=$5
RewriteRule ^news/([^/].)/([^/].)/?page=(.)$ /news.php?$1=$2&page=$3
RewriteRule ^news/([^/])/([^/])/([^/])/([^/])/$ /news.php?$1=$2&$3=$4
RewriteRule ^news/([^/].)/([^/].)/$ /news.php?$1=$2
RewriteRule ^news/$ /news.php

InsanityNet
why the downvote?
InsanityNet