skip to Main Content

I have a URL

 http://localhost/xxxx/cat/History?randomArg=2

that I want to run

http://localhost/xxxx/cat/edit.php?name=History&randomArg=2

 http://localhost/xxxx/cat.php?name=History&randomArg=2          

(POST EDIT#2 Correction I did originally ask for edit.php but I meant cat.php)

and I have a RewriteRule that works on my live server

RewriteRule "^(.*)xxxx.*/cat/(.*)?(.*)$" $1/xxxx/cat.php?name=$2&t=123$3[QSA]

and when I run a test via the htaccess tester https://htaccess.madewithlove.com?share=f9987308-2570-4fbe-a769-4e5031a96578, I get…

RewriteRule "^(.*)xxxx.*/cat/(.*)?(.*)$" $1/xxxx/cat.php?name=$2&t=123$3[QSA]


enter image description here

Firstly, I can’t see why $3 (e.g. "randomArg=2") isn’t coming through on the https://htaccess.madewithlove.com tester site.

Secondly, I have plugged this into my WAMP environment and although I see cat.php running I don’t see the RewriteRule working**

my cat.php code reads:

echo "<LI>_GET[*]:<PRE>" . print_r($_GET, true) . "</PRE>";
echo "<LI>_SERVER[SCRIPT_NAME]:<PRE>" . $_SERVER['SCRIPT_NAME'] . "</PRE>";
echo "<LI>_SERVER[REQUEST_URI]:<PRE>" . $_SERVER['REQUEST_URI'] . "</PRE>";

if (isset($_GET['name'])) {
    echo "<LI>GET[name]=<PRE>" . $_GET['name'] . "</PRE>";
    $params = explode("/", $_GET['name']);
    $site = array_shift($params);
    echo "<LI>shifted:[$site]";
}
else echo "<LI>No GET[name]";

if (isset($_GET['t'])) echo "<LI>t:<PRE>" . $_GET['t'] . "</PRE>";
else echo "<LI>No GET[t]";

and the output for http://localhost/xxxx/cat/History?randomArg=2 reads:

_GET[*]:Array
(
    [randomArg] => 2
)
_SERVER[SCRIPT_NAME]:/xxxx/cat.php
_SERVER[REQUEST_URI]:/xxxx/cat/History?randomArg=2
No GET[name]
No GET[t]

** But if the rule isn’t working then why is cat.php running (as the URL asks for "cat/History"?

(Windows, Apache 2.4.41, PHP5.4)



      As a sidenote/test, putting this into my LAMP (Apache 2.4.6) environment using a similar rule (but using sss.xxx.com/testHtaccess/History) with the following rule…

      RewriteRule "^(.*)testHtaccess/(.*)?(.*)$" $1/testHtaccess.php?name=$2&t=123$3[QSA]
      

      … does partially work (it passes "name" through, but still no $3)!


So how can I get my localhost rule to work?

** ADDITIONAL (POST EDIT#1):

For what it’s worth, and as it’s pointed out by anubhava (below) I notice I have the following httpd-vhosts.conf default settings:

 # Virtual Hosts
 #
 <VirtualHost *:80>
   ServerName localhost
   ServerAlias localhost
   DocumentRoot "${INSTALL_DIR}/www"
   <Directory "${INSTALL_DIR}/www/">
     Options +Indexes +Includes +FollowSymLinks +MultiViews
     AllowOverride All
     Require local
   </Directory>
 </VirtualHost>

+MultiViews will explain "cat" turning into "cat.php"

2

Answers


  1. Chosen as BEST ANSWER

    Ok - lots of random variation attempts seems [and based on anubhava's very gratefully received persistence] to have found the following works:

    RewriteRule "^(cat)/([w- ]+)/?.*$" xxxx/cat.php?name=$2&t=12345 [L,QSA,NC]
    

    although https://htaccess.madewithlove.com/ tells me that the rule was not met! :-(

    (I wish there was a more scientific way to develop RewriteRules)

    So using the rule (above), and running http://localhost/xxxx/cat/Histor5ys?o=91 with the following php:

    echo "<LI>_GET[*]:<PRE>" . print_r($_GET, true) . "</PRE>";
    echo "<LI>_SERVER[SCRIPT_NAME]:<PRE>" . $_SERVER['SCRIPT_NAME'] . "</PRE>";
    echo "<LI>_SERVER[REQUEST_URI]:<PRE>" . $_SERVER['REQUEST_URI'] . "</PRE>";
    
    
    if (isset($_GET['name'])) {
        echo "<LI>GET[name]=<PRE>" . $_GET['name'] . "</PRE>";
    }
    else echo "<LI>No GET[name]";
    
    
    if (isset($_GET['t'])) echo "<LI>t:<PRE>" . $_GET['t'] . "</PRE>";
    else echo "<LI>No GET[t]";
    
    
    if (isset($_GET['o'])) echo "<LI>o:<PRE>" . $_GET['o'] . "</PRE>";
    else echo "<LI>No GET[o]";
    

    outputs ...

    _GET[*]:Array
    (
        [name] => Histor5ys
        [t] => 123456
        [o] => 91
    )
    _SERVER[SCRIPT_NAME]:/xxxx/cat.php
    _SERVER[REQUEST_URI]:/xxxx/cat/Histor5ys?o=91
    GET[name]=Histor5ys
    shifted:[Histor5ys]
    t:123456
    o:91
    

    • You cannot match query string using RewriteRule (though you don’t need to match here).
    • You appear to have MultiViews (content negotiation service) turned on.

    You can use this code in your site root .htaccess:

    Options -MultiViews
    RewriteEngine On
    
    RewriteCond ^(.*/cat)/([w-]+)/?$ [NC]
    RewriteRule ^ %1.php?name=%2 [L,QSA,NC]
    

    Using QSA your original query string will automatically be appended to new target.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search