0

Here's my .htaccess rule which works:

RewriteCond %{REQUEST_URI}   ^/work
RewriteCond %{QUERY_STRING}   ^foo=bar$
RewriteRule  ^(.*)$   /test.php

Unfortunately, if I want to append some parameters to the rewritten path it doesn't work:

RewriteCond %{REQUEST_URI}   ^/work
RewriteCond %{QUERY_STRING}   ^foo=bar$
RewriteRule  ^(.*)$   /test.php?par=new

If I add [R=301] then it works too, but I don't want to add it because I don't want the user see the address changed:

RewriteCond %{REQUEST_URI}   ^/work
RewriteCond %{QUERY_STRING}   ^foo=bar$
RewriteRule  ^(.*)$   /test.php?par=new   [R=301]

Why isn't the 2nd one working? Did I miss something? Environment: Ubuntu 12.04, XAMPP(Apache/2.4.4)

Thanks.

Edit 1

Per Internal URL rewrite no longer working after upgrading Apache to 2.4, this seems to be a bug related to Apache 2.4.

.htaccess:

RewriteCond %{QUERY_STRING} ^foo=bar$ 
RewriteRule ^work/(.*)$ /test.php?par=new   [L]

log when execute curl http://alison/work/?foo=bar -v

[Thu Aug 28 15:36:39.728123 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1e05820/initial] [perdir /home/amareknight/Projects/firstmg/root/alison/] strip per-dir prefix: /home/amareknight/Projects/firstmg/root/alison/work/ -> work/
[Thu Aug 28 15:36:39.728141 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1e05820/initial] [perdir /home/amareknight/Projects/firstmg/root/alison/] applying pattern '^work(.*)$' to uri 'work/'
[Thu Aug 28 15:36:39.728170 2014] [rewrite:trace4] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1e05820/initial] [perdir /home/amareknight/Projects/firstmg/root/alison/] RewriteCond: input='foo=bar' pattern='^foo=bar$' => matched
[Thu Aug 28 15:36:39.728186 2014] [rewrite:trace2] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1e05820/initial] [perdir /home/amareknight/Projects/firstmg/root/alison/] rewrite 'work/' -> '/test.php?par=new'
[Thu Aug 28 15:36:39.728201 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1e05820/initial] split uri=/test.php?par=new -> uri=/test.php, args=par=new
[Thu Aug 28 15:36:39.728218 2014] [rewrite:trace1] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1e05820/initial] [perdir /home/amareknight/Projects/firstmg/root/alison/] internal redirect with /test.php [INTERNAL REDIRECT]
[Thu Aug 28 15:36:39.728420 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] strip per-dir prefix: /home/amareknight/Projects/firstmg/root/alison/work/index.html -> work/index.html
[Thu Aug 28 15:36:39.728436 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] applying pattern '^work(.*)$' to uri 'work/index.html'
[Thu Aug 28 15:36:39.728457 2014] [rewrite:trace4] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] RewriteCond: input='par=new' pattern='^foo=bar$' => not-matched
[Thu Aug 28 15:36:39.728473 2014] [rewrite:trace1] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] pass through /home/amareknight/Projects/firstmg/root/alison/work/index.html
[Thu Aug 28 15:36:39.728624 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1f9a360/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] strip per-dir prefix: /home/amareknight/Projects/firstmg/root/alison/work/index.html.var -> work/index.html.var
[Thu Aug 28 15:36:39.728640 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1f9a360/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] applying pattern '^work(.*)$' to uri 'work/index.html.var'
[Thu Aug 28 15:36:39.728660 2014] [rewrite:trace4] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1f9a360/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] RewriteCond: input='par=new' pattern='^foo=bar$' => not-matched
[Thu Aug 28 15:36:39.728675 2014] [rewrite:trace1] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1f9a360/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] pass through /home/amareknight/Projects/firstmg/root/alison/work/index.html.var
[Thu Aug 28 15:36:39.728741 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] strip per-dir prefix: /home/amareknight/Projects/firstmg/root/alison/work/index.php -> work/index.php
[Thu Aug 28 15:36:39.728760 2014] [rewrite:trace3] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] applying pattern '^work(.*)$' to uri 'work/index.php'
[Thu Aug 28 15:36:39.728779 2014] [rewrite:trace4] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] RewriteCond: input='par=new' pattern='^foo=bar$' => not-matched
[Thu Aug 28 15:36:39.728794 2014] [rewrite:trace1] [pid 14865] mod_rewrite.c(468): [client 127.0.0.1:33092] 127.0.0.1 - - [alison/sid#1b438a0][rid#1fa24a0/subreq] [perdir /home/amareknight/Projects/firstmg/root/alison/] pass through /home/amareknight/Projects/firstmg/root/alison/work/index.php

I have index.php located in /work directory and it seems Apache just ignores the INTERNEL_REDIRECT and loads the index.php.

Community
  • 1
  • 1
ChandlerQ
  • 1,428
  • 2
  • 21
  • 29
  • 1
    have you tried using `[QSA,L]`? Also if you use `QSA` and the URL previously had `?par=new` in it, you don't even need to append it again, as it will be done automatically. – Prix Aug 27 '14 at 12:38
  • 1
    Are there more rules in your .htaccess? – anubhava Aug 27 '14 at 12:40
  • @Prix The original URL doesn't have `?par=new`, I just want to append it. – ChandlerQ Aug 28 '14 at 02:22

1 Answers1

1

You can have your rule like this in your root .htaccess:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^foo=bar$ [NC]
RewriteRule ^work /test.php?par=new [L,QSA,NC]

This will internally rewrite a URL like /work/abc?foo=bar to /test.php?foo=bar&par=new

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Doesn't work for me. Actually I think it's a bug related to Apache 2.4 because the same .htaccess works on Apache 2.2. – ChandlerQ Aug 28 '14 at 07:37
  • What is the URL you are seeing in your browser and where is this .htaccess located? – anubhava Aug 28 '14 at 07:41
  • I don't use browser to test, I just use curl: `curl "http://alison/work/?foo=bar" -v` – ChandlerQ Aug 28 '14 at 07:45
  • Yes curl is better to test than browsers since it doesn't cache results. – anubhava Aug 28 '14 at 07:49
  • Per https://issues.apache.org/bugzilla/show_bug.cgi?id=53929 this issue appears again in 2.4.6 and fixed in 2.4.9, I'll give it a shot. Thanks for your help. – ChandlerQ Aug 28 '14 at 07:51
  • ok for testing if you add `R` in the rule to make it: `RewriteRule ^work /test.php?par=new [L,QSA,NC,R]` does it redirect to: `http://alison/test.php?foo=bar&par=new` ? – anubhava Aug 28 '14 at 08:05
  • Yes, `R` works, but that will return a 302 redirect and I don't want the visitor see `http://alison/test.php?foo=bar&par=new` in his address bar. – ChandlerQ Aug 28 '14 at 08:11
  • Also, if I specify "index.php" when `curl` then it works too. `curl "http://alison/work/index.php?foo=bar" -v` – ChandlerQ Aug 28 '14 at 08:12
  • So `http://alison/work/index.php?foo=bar` gets rewritten but `http://alison/work/?foo=bar` gives 404? – anubhava Aug 28 '14 at 08:16
  • `http://alison/work/index.php?foo=bar` get rewritten to `http://alison/test.php?foo=bar&par=new` but `http://alison/work/?foo=bar` actually returns `http://alison/work/index.php`, I posted the full log in my question. – ChandlerQ Aug 28 '14 at 08:20
  • No, this is the only .htaccess I have. I've tested the same project on 2.2.16 and it works. – ChandlerQ Aug 28 '14 at 08:23
  • Ok try putting `Options -MultiViews` on top of your .htaccess and retest – anubhava Aug 28 '14 at 08:26
  • @AmareKnight do you really have no other rules on your `.htaccess` file? it looks to me that the first rule is processed and then it loops on the next rules, can you update your question with your `.htaccess` exactly as it is on the root folder? Also look inside the work folder and make sure there is no other `.htaccess` with `ls -la` to make sure your viewing files that start with a dot. – Prix Aug 28 '14 at 09:03
  • @AmareKnight instead of `[L,QSA,NC]` try `[END,QSA,NC]` which should make HTTPD also stop any subsequent rewrites, giving you're using version 2.4+ – Prix Aug 28 '14 at 09:06
  • @Prix That's exactly why it's a bug with Apache 2.4.6. You're right about Apache continues loop, but it's trying to find index.html, index.html.var, index.php... All these files are only defined in httpd.conf as DirectoryIndex by default. – ChandlerQ Aug 28 '14 at 09:10
  • @AmareKnight and have you upgraded it or tried to use the END flag? – Prix Aug 28 '14 at 09:10
  • I tried reverting back to 2.2.16 but haven't tried upgrading to 2.4.9. – ChandlerQ Aug 28 '14 at 09:11
  • 1
    @AmareKnight could you update your question with an exact copy of your `.htaccess` file? – Prix Aug 28 '14 at 09:14
  • In addition to disabling `MultiViews` also use this line on top of your .htaccess `DirectoryIndex index.php` – anubhava Aug 28 '14 at 09:28
  • @Prix Can't edit any posts now... Alert "Page Not Found" – ChandlerQ Aug 28 '14 at 09:36
  • @anubhava This works, but what about other index.htmls? – ChandlerQ Aug 28 '14 at 09:37
  • Can you have `index.php` and/or `index.html` both in same directory? If yes then use **`DirectoryIndex index.php index.html`** – anubhava Aug 28 '14 at 09:43
  • Upgrade to 2.4.10 and problem solved too. Definitely a bug with Apache itself. Thanks all the same @Prix – ChandlerQ Aug 28 '14 at 10:11
  • 1
    @AmareKnight we do try to exhaust possibilities before calling it bug ;) but glad you got it solved. – Prix Aug 28 '14 at 10:12