Solved

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

Posted on 2009-07-08
12
842 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:mobrien118
  • 7
  • 4
12 Comments
 
LVL 14

Expert Comment

by:profya
Comment Utility
0
 
LVL 14

Expert Comment

by:profya
Comment Utility
And this explains conditions to rewrite rules:
http://www.askapache.com/htaccess/mod_rewrite-tips-and-tricks.html
0
 

Author Comment

by:mobrien118
Comment Utility
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
 
LVL 14

Expert Comment

by:profya
Comment Utility
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
 

Author Comment

by:mobrien118
Comment Utility
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
 
LVL 1

Expert Comment

by:seraph_matrix_631
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Accepted Solution

by:
profya earned 500 total points
Comment Utility
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
 
LVL 14

Expert Comment

by:profya
Comment Utility
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
 

Author Comment

by:mobrien118
Comment Utility
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
 

Author Closing Comment

by:mobrien118
Comment Utility
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
 
LVL 14

Expert Comment

by:profya
Comment Utility
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
 
LVL 14

Expert Comment

by:profya
Comment Utility
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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now