Forum Moderators: phranque

Message Too Old, No Replies

Redirect Entire URL to a get variable

         

augury

6:27 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



I am basically trying to take /folder1/folder2/page
to index.php?url=/folder1/folder2/page
I want this done whenever the page doesn't exist.
I am normally pretty good with rewrite rules but none of my attempts have worked.

This was my most recent attempt:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\[ \]]+)/?$ index.php?url=$1 [L]

Aug

augury

7:53 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



I am still at a loss. I have tried changing the Condition to:
RewriteCond %{REQUEST_URI} !^/(images¦design¦index.php.*)$
To just not redirect on the images and design folder. But the RewriteRule appears to not be working.

Does anyone have any advice?

jdMorgan

8:03 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is not the rule but its pattern that is the problem here.

If you want to rewrite any-two-directory-levels-followed-by-any-non-blank-nonexistent-filename, then try:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/){2}[^/]+)$ index.php?url=$1 [L]

That pattern means "Match anything but a slash, followed by a slash (twice) followed by anything but a slash."

Jim

g1smd

8:14 pm on Jun 22, 2009 (gmt 0)

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



This will, of course, means that your site produces 'Infinite Duplicate Content" unless the PHP script is also sending out the HTTP HEADER with a 404 or 410 status code.

augury

9:01 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



The subfolders could potentially be in the 10s or 20s. I tried the rewrite exactly as you have placed it and it doesn't work for even one subfolders.

The php script is using the url to determine which information to serve.

jdMorgan

9:25 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I used your first example to derive the pattern. As stated, there must be exactly two subdirectory levels for that rule to work -- The requested URL must be of the form "example.com/dir1/dir2/filename".

If you want "ten to twenty" directory levels, then that would be


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/){10,20}[^/]+)$ index.php?url=$1 [L]

but I don't suppose that's exactly what you want, either. If you will be very specific and thorough in describing your exact requirements, you'll get a better/faster answer.

Also, we're assuming that you've got other working rules in this file, and therefore have the requisite directive(s) to enable mod_rewrite and the rewriting engine.

As g1smd noted, the php script must check that it does in fact have something specific to serve, and if not, should return a 404-Not found status response. Otherwise, you'll likely have duplicate-content problems with search engines. If a competitor discovers the 'hole,' they can exploit it to intentionally give you duplicate-content problems...

Jim

augury

10:33 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



Yes I have just verified that the rewrite engine is working.

The idea is a url is based that is that matches the location string in a database.
The string maybe:
/folder1/folder2/name
/name
/folder1/folder2/.../folder20/name

Whatever the url is I just need to send it to index.php?url=

augury

10:34 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



If the url doesn't exists the system does redirect 301 to a 404 page.

The rewrite rule at the top works correctly in IIS based installations (using the rewrite module) of this system, just can't get it to work in apache.

g1smd

10:46 pm on Jun 22, 2009 (gmt 0)

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



*** If the url doesn't exists the system does redirect 301 to a 404 page. ***

I don't like the words "redirect to a 404 page". It usually spells Trouble.

jdMorgan

11:03 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I stated, the php script must produce a 404 status response -- *not* any kind of redirect... Unless you do not care about your search rankings.

Jim

augury

12:01 am on Jun 23, 2009 (gmt 0)

10+ Year Member



You guys are 100% hung up on the wrong aspect of this.

g1smd

1:33 am on Jun 23, 2009 (gmt 0)

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



No. We like to consider the *whole* picture. We also like to give the right answer to the right question, rather than the right answer to the wrong question; and there a lot of wrong questions here.

We're also aware that some 80% of posters don't know the difference between a redirect and a rewrite - even though that's what they are trying to configure their server to do.

jdMorgan

1:43 am on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possibly, but it's because it's important. We're also kind of hung up on proper terminology and clear communication, because that's important as well: Computers do what you tell them to do, and not necessarily what you want them to do -- or what you should want them to do.

So things like the fact that this is not an application for any kind of redirect (but rather for an internal rewrite) matter quite a bit. And the disposition of URLs for which the script can find no appropriate content is critical; We would do you no favor by letting you proceed with generating a "redirect to a 404 page" without comment here, because that could very well sink your site in the SERPs. You might well have left with a pretty little rewrite rule, and been out of business in six months.

The remaining "right aspect of this" is rather trivial. If you want *all* URLs which do not resolve to a physical file to be forwarded to the script, just change the RewriteRule pattern to "^(.*)$". If you want all non-blank URLs which do not resolve to a physical file to be forwarded to the script, change the RewriteRule pattern to "^(.+)$". If you want to make sure that no URL *ending* with a slash (i.e. a directory request) is forwarded to the script, then use "^(.*[^/])$".

Jim

[edited by: jdMorgan at 4:25 am (utc) on June 23, 2009]

augury

6:18 am on Jun 23, 2009 (gmt 0)

10+ Year Member



^(.*[^/])$ Ended up doing the trick.
I do appreciate all the help, even if I did get frustrated.
I also understand why you both offered the advice you did.

Aug