Forum Moderators: phranque

Message Too Old, No Replies

php rewrite rule to allow 301 redirects on Apache Server

cleaning up bad php scripts by deleting and rewriting

         

SEOptimism

7:29 am on Nov 29, 2008 (gmt 0)

10+ Year Member



I have a question about a php rewrite rule - I dont' know php or rewrite rules, but can adapt with simple instructions. Maybe someone can suggest a solution that isn't too complex?

I've had php scripts running on my site which created URL's that had a structure where there were 8 or 9 adaptations of a page that changed (http://foo.com/directory/filename.html) by inserting a simple: "1thing.php?a=" before an existing folder and filename and presented many variations on those pages:

[foo.com...] X 900 filenames
[foo.com...] X 900 filenames
[foo.com...] X 900 filenames

There were about 8 of these variations.

I've deleted those scripts and want to redirect *some* of them to flat HTML pages, but there are about 900 possible variations in folder and filenames in a flat html site. That means there are about 900 variations for EACH of those 8 scripts totaling 7200 variations which I've deleted from my server.

Is it possible to rewrite this to redirect in a way that sends visitors from these:

[foo.com...]
[foo.com...]

to these:

[foo.com...]
[foo.com...]

I'd like to have this apply only specific filenames and not necessarily all 900 of them. In other words, I'm only creating a limited number of flat html pages that I will redirect to, so will manually list only those files I want to redirect to and let the rest fail to 404 errors.

(I have a current rewrite rule to force the non-www version of my url, just like the example given I mention this because I've seen several rewrites forcing www in the URL and I don't want to force to www.)

jdMorgan

8:42 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll either need to write separate rules for each of the possible variations of the 'fields' in the URL, or you will have to check to see if the replacement URL will resolve to an existing html file. The first solution requires an awful lot of repetitious and error-prone coding, while the second solution *may* put a lot of load on your server, since every request for a "thing.php" URL will result in a call to the operating system to go check the disk to see if the corresponding html file exists.

If I take your example URLs literally, then the 'mapping' of old to new URLs could be done with something like:


RewriteCond %{QUERY_STRING} &?a=([^.]+)\.html&?
RewriteCond %{DOCUMENT_ROOT}/%1-$1.html -f
RewriteRule ^([^./]+)\.php$ http://example.com/%1-$1.html [R=301,L]

See the resources cited in our Forum Charter and the threads in our Forum Library for more information. It is critical to have a basic understanding of regular-expressions patterns and mod_rewrite directives in order to debug and maintain code like this.

Jim