skip to Main Content

I’m trying to write REWRITERULE for NEW url.


old url : https://www.ex.com/hindu-bbb-10/2

old htaccess:

RewriteCond %{HTTP_HOST} ex.com$ [NC]
RewriteCond %{REQUEST_URI} (bbb|ggg)
RewriteRule ^(.+?)/(.+?)(/?)$ seo/index.php?cat=$2&title=$1 [NC,L]

by using above htaccess I could get values from REQUEST like as folllows

[cat] => 2 [title] => hindu-bbb-10

I tried lot and one is as follows

NEW URL : https://www.ex.com/hindu-bbb-10?pageno=2

NEW htaccess:

RewriteCond %{HTTP_HOST} ex.com$ [NC]
RewriteCond %{REQUEST_URI} (bbb|ggg)
RewriteRule ^(.+?)/(.+?)(.+?)$ seo/index.php?cat=$2?pageno=$1 [NC,L]

And getting result like this [cat] => hindu-bbb-10

I can’t get values like [cat] => 2 [title] => hindu-bbb-10 which is form old url.

How to get values like [cat] => 2 [title] => hindu-bbb-10 using new url “https://www.ex.com/hindu-bbb-10?pageno=2

2

Answers


  1. If you’re wanting the result to be:

    https://www.ex.com/hindu-bbb-10?pageno=2
    

    Then you will have to change your rewrite rule to accomodate that. You have got the right idea of doing it but when you want certain variables to be used you can use $2 and $1 as you have done.

    The rewrite rule should be something along the lines of

    RewriteRule ^(.+?)/(.+?)(.+?)$ seo/index.php?title=$2?pageno=$1 [NC,L]
    

    If you would like to change cat to pageno=2 I would create an array by using the variables you have managed to get. E.g.

    $arr = ['title' => $_REQUEST['title'], 'pageno' => $_REQUEST['cat'];
    

    This should change your url to how you would like it. If this doesn’t work then I would advice you to read the .htaccess documentation:

    https://httpd.apache.org/docs/current/howto/htaccess.html

    Login or Signup to reply.
  2. For this URL https://www.ex.com/hindu-bbb-10?pageno=2

    You need to capture value from QUERY_STRING:

    RewriteCond %{HTTP_HOST} ex.com$ [NC]
    RewriteCond %{REQUEST_URI} (?:bbb|ggg)
    RewriteCond %{QUERY_STRING} ^pageno=(d+) [NC]
    RewriteRule ^([^/]+)/?$ seo/index.php?cat=%1&&title=$1 [NC,QSA,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search