We help IT Professionals succeed at work.

Apache (Apache2) redirect everything but root (/index.php) as a parameter in a second script using mod_rewrite?

mobrien118 asked
Medium Priority
Last Modified: 2012-05-07

I'm having some difficulty getting apache to behave how I want it to.

I got my virtual server working so that requests from a separate A record all go to it, and others go to the other main one, so that's great. I'm having a problem getting my mod_rewrite to work properly.

I had this working on the old server (where I wasn't using the other A record):
      RewriteRule    ^/g/(.*)  /gurl.php?q=$1
For instance, that would take "http://www.example.com/g/QWERTY" and translate it to "http://www.example.com/gurl.php?q=QWERTY".

This was great, but I want the URLs to be even shorter, so I got a new URL and now the files are on the root. So, I want there to be 2 possible scripts for this: / and /STRING (where STRING is any possible word.

So, I tried this:
      RewriteRule    ^/(.+)  /gurl.php?q=$1
      RewriteRule      ^/$      /index.php
But, it isn't working. What I want to happen is for ANYTHING after the / (root) to be translated and fed as a parameter to the script. If there is nothing after the / it should just show the index page.

Well, the index page is working (but it worked before this whole thing, so...).

Can anyone tellme what I am doing wrong, or if what I'm trying to do is possible? If I have to, I'll go back to having the "/g/" part in the URL, but the whole point of this is to make short URLs, so the more I can take out, the better.

Thanks in advance,

Watch Question

And this explains conditions to rewrite rules:


I wonder is what I'm trying to do just can't be done. I've read through all of apache's documentation, I am familiar with Regular Expressions. Here is my whole virtual directory file (attached in "code" section) from the Inside of the <VirtualDirectory> tags.

Also, I pretty well understand mod_rewrite, and there isn't anything in that "tips and tricks" that jumps out at me (yes, I read it though) as anything that I didn't already know. I think I understand the "book version" of mod_rewrite, it's the practice and expertise that I lack, which is why I came to the experts!

Strangely, it doesn't seem like any redirects are working. I have "RewriteEngine on" in the main "rewrite.conf" file, which worked for the other virtual directory, so it should for this one as well. Maybe something else is wrong...

Ahhhh! This one is so frustrating!

BTW, That generator is giving me the same thing I have *sort of*, but it is randomly adding back references that don't need to be there. It may be harder for me to figure out that generator than to just ave someone point out where my error is.
	DocumentRoot /var/www/gurl
	ServerName [removed for security]
	<Directory /var/www/gurl>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride all
		Order allow,deny
		allow from all
		RewriteRule    ^/(.+)$  /gurl.php?q=$1
		RewriteRule	^/$	/index.php
	LogLevel warn

Open in new window

Ok, I found a good solution. Let's divide the problem into two, the first case is redirection of all directory/page references, such as mydomain.com/test ->mudomain.com/gurl.php?q=test. This problem is solved with the code snipped attached.
The second problem is access the document root without specifying the directory or a page, in this case we can use directoryindex directive. Try this solution, I am using .htaccess.
Options Indexes FollowSymLinks MultiViews
RewriteEngine On
DirectoryIndex mynewindexpage.html
RewriteRule    ^([^gurl.php?q=].*)$  /gurl.php?q=$1

Open in new window


profya! That is very close! Very clever on matching the optional "gurl.php?q=" to avoid recursion!

I thought it was working for a second, but alas, it isn't.

Now, when I go to "http://mysite/" it redirects to "http://mysite/gurl.php?q=index.php"   (since I set index.php as my "DirectoryIndex").

I even tried adding the "RewriteRule"s to match the root (again) trying to match on either "^/$" or just "^$" and neither of them worked. Oh, and I used the [L] switch for those rules, indicating that the rewrite should stop there and not continue on th the next rule which would send them to the gurl.php page. Could apache be caching the file for some reason? I tried using apache2ctl restart (which worked) and that didn't change it. I also tried using a different browser and that didn't work either.

It seems like the redirects are working, though!

Any other ideas on getting the index to work?
could you not use PHP at the start of the index.php page to get the URL
then append the name into a redirect statement?


function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

header( 'Location: http://www.mydomain.com/gurl.php?q=curPageName()' );
Unlock this solution and get a sample of our free trial.
(No credit card required)

The index page, test.html has been now excluded too. I tested on my machine worked fine, I hope it does so with you. :)


profya, That is some more genius, but again, it's not working, and I just can't figure out why!!!

I'll give you a little more background, it's basically a "short URL" program, that I can put a URL into, then it generates a random 6 character key and stores it in a database.

I have "debug" turned on in the logs, and "http://mysite/QWERTY" still trying to hit "/my_web_root/QWERTY"!

So, when I generate a new "short URL" it presents the link. When I click on it, it sends me to the right place, but it doesn't work. I'm getting 404's and apache is reporting "File does not exist: /my_web_root/QWERTY, referrer: http://mysite/"

(FYI: I have exactly what you wrote in my .htaccess, except that I'm using index.php rather than test.html)

This is so frustrating! I really think your solution should work!

@seraph: As I have now revealed, it is a URL program, so yes, I could consolidate it down to 1 script, but it would involve a decent rewrite and a rather large "if" block. Also, if I end up wanting to put more things on the index page, it would further complicate the whole thing. I'd just rather get the redirect working how it should be! If I have to, I'll end up doing that though.


Never mind. It seems to be working now. Sorry for the confusion.

I'm not sure why, but it seems like a FEW redirects aren't hitting. I am very confused at this point, but I think I'll just monitor it. It may have something to do with caching or something, why some URLs are breaking, so hopefully it will clear up.

If you want to try to use the application, it's at http://gurl.hopto.org.

It's just a little experiment I wanted to try. The funny thing is that it only took me less than 2 hours to write the scripts and set up the database and getting the rewrites to work on my longer URL, but I spent forever getting this new URL to behave!

Thanks so much for your help. Points awarded!

Good progress has been done, I hope this will not fail:
Options Indexes FollowSymLinks MultiViews
RewriteEngine On
DirectoryIndex test.html
RewriteRule    (.*[^{gurl.php?q|test.html}].*)  /gurl.php?q=$1

Open in new window

Thank you too, I will be ready to respond if something went wrong while you are monitoring, I'll test it online and see. :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.