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

Hello,

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,

--mobrien118
mobrien118Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

profyaCommented:
0
profyaCommented:
And this explains conditions to rewrite rules:
http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html
0
mobrien118Author Commented:
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
	</Directory>
	
	LogLevel warn

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

profyaCommented:
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

0
mobrien118Author Commented:
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?
0
seraph_matrix_631Commented:
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?

so:

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


header( 'Location: http://www.mydomain.com/gurl.php?q=curPageName()' );
?>
0
profyaCommented:
This will fix it:
Options Indexes FollowSymLinks MultiViews
RewriteEngine On
DirectoryIndex test.html
RewriteRule    ^([^{gurl.php?q|test.html}].*)$  /gurl.php?q=$1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
profyaCommented:
The index page, test.html has been now excluded too. I tested on my machine worked fine, I hope it does so with you. :)
0
mobrien118Author Commented:
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.
0
mobrien118Author Commented:
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!
0
profyaCommented:
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

0
profyaCommented:
Thank you too, I will be ready to respond if something went wrong while you are monitoring, I'll test it online and see. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.