Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule where URL contains ? character

         

runner

9:07 pm on Sep 17, 2015 (gmt 0)

10+ Year Member



I have spent the last two hours searching the internet for a solution to this with no luck.

I have a URL that calls a cgi script and that URL contains a ? character. See below:

/DirectoryName/CGIscript.cgi?param=5

All I want to do is change the directory name to all lower case and leave the rest of the URL alone. See below:

/directoryname/CGIscript.cgi?param=5

I have tried multiple RewriteRules but in every case, the rewritten URL always has the string "CGIscript.cgi" removed from it. I found several posts that suggest the ? in the URL is causing the problem. I have found no solution so far. Does anybody have any suggestions?

runner

9:18 pm on Sep 17, 2015 (gmt 0)

10+ Year Member



I should have included some of the Rewrite Rules I have tried. This is the one I thought would work:

RewriteRule ^/DirectoryName/(.*)$ /directoryname/$1

lucy24

9:55 pm on Sep 17, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I smell a red herring.

The ? in the URL introduces a query string. By default, mod_rewrite silently reappends the query after doing whatever else you've told it to do. So all you have to do is ignore everything from the question mark on and just write the rule for
DirectoryName/blahblah
or, as it were,
DirectoryName/(.*)
which appears to be what you're already doing.

But if this is happening in htaccess, you have to omit the leading / slash in the pattern. That's why I suspected a red herring.

Edit: When you're making an external redirect, always include the full protocol-plus-domain in the target, and don't forget the [R=301,L] flags. (You may have just left that out when posting.)

runner

3:28 am on Sep 18, 2015 (gmt 0)

10+ Year Member



This is a local redirect so I didn't include the protocol. It's not in a .htaccess file but I will try with and without the leading slash. Thank you for the info. I will try this when I get to work in the morning.

runner

3:43 am on Sep 18, 2015 (gmt 0)

10+ Year Member



Nope, that doesn't work. It still removes the name of the cgi script.

RewriteRule ^DirectoryName/(.*) /directoryname/$1[R=301,L]

The result is that it changes:
/DirectoryName/CGIscript.cgi?param=5
to
/directoryname/?param=5

It does change the directory name to all lower case bit it also removes the name of the cgi script.

lucy24

7:05 am on Sep 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That is very weird, and makes me wonder if there's some unrelated rule, elsewhere, that you'd forgotten all about. "CGIscript.cgi" isn't your DirectoryIndex filename, is it?

whitespace

8:57 am on Sep 18, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Nope, that doesn't work. It still removes the name of the cgi script.


Well, both those directives (one with and one without the slash prefix on the RewriteRule pattern) cannot both match - one of those should not do anything at all. So, either you are seeing a cached response or there is something else doing this, as lucy24 suggests.

(301 redirects are cached by the browser - including ones that didn't work properly(!) - so either test with 302's or make sure you are testing with the browser cache disabled.)

wilderness

12:59 pm on Sep 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



FWIW
This is a local redirect so I didn't include the protocol. It's not in a .htaccess file but I will try with and without the leading slash.


So that the folks here might assist you correctly?
If your not using htaccess, than what method are you using?
PHP, CGI or something else?

runner

2:54 pm on Sep 18, 2015 (gmt 0)

10+ Year Member



The rewrite rule is located in the virtual host's conf file under /etc/apache2/sites-enabled. I've never tried placing rewrite rules in an .htaccess file before. We don't normally use .htaccess files.

I tried different web browsers to see if it might be cached. Nothing worked. Cleared cache etc.

This might not be able to be done with a one line rewrite rule. There are other files in the destination directory, not just cgi scripts. The rewrite rule above works fine when there is no cgi script involved. I might have to determine how many different cgi scripts there are and then check the URL to see if it contains a specific cgi script and then craft a rewrite rule with that cgi script name hard coded in the destination. That would solve the problem. However, I'd have to have a rewrite rule for each cgi script. I think it would work.

lucy24

8:57 pm on Sep 18, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A lot of times-- including here-- "in htaccess" really means "in any directory section". Either way, the beginning of the physical path-- including the last directory slash-- is stripped off.

Here, it's clear that the rule is working: DirectoryName is redirected to directoryname. So what we need to figure out is what's happening to the "CGIscript.cgi" part. Does it have a lot of different values, all of which somehow get stripped off when the redirect takes place?

Now, since you are working in the config file, you've also got the option of running a RewriteLog. It will take some experimenting to set the most useful logging level (RewriteLogLevel directive); no matter what number you choose, the log will include a bunch of extraneous ### along with the information you need. But this will let you see exactly what request meets mod_rewrite, and what happens to it along the way.

Incidentally: Unless you've got a really wonky browser, hitting Refresh should force a new request to the server. So I wouldn't worry a lot about cached responses. (If you're working on a test site-- or a test directory within a live site-- you can set caching for pages to "Access", forcing a fresh request every time. Obviously this isn't practical on a live site, unless your content really does change every few seconds.)