2

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
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
user350230
  • 73
  • 1
  • 7

2 Answers2

3

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
  • 4,983
  • 19
  • 36
1

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.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844