Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite help

apache, mod_rewrite, .htaccess

         

skinter

12:27 am on Oct 19, 2005 (gmt 0)

10+ Year Member



I'm trying to get a script running that will redirect an obsolete file, with the newer one. Since the old file (and new) is PHP, the htaccess file won't redirect the query strings, so I was trying to make a condition that would for the ease of my readers and search engines. Sadly, I failed, and the three lines gave me a 500 error. The code is as follows:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/file\.php\?id=([a-z0-9)+]$ [NC]
RewriteRule ^/\?id=([a-z0-9]+)$ [R=301]

What am I doing wrong? The URL's are /file.php, and just / . Thanks in advance for the help.

jdMorgan

2:36 am on Oct 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to post an example old and new local URL-path.

Jim

skinter

3:15 am on Oct 19, 2005 (gmt 0)

10+ Year Member



I want it to redirect /file.php?id=variable to /?id=variable.

jdMorgan

5:05 am on Oct 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to test the query string and also prevent an infinite redirection loop due to the action of DirectoryIndex pointing "/" back to "file.php", you can use the Apache variable THE_REQUEST:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /file\.php\?id=[^&\ ]+
RewriteRule ^file\.php$ http://www.example.com/?id=%1 [R=301,L]

An example of what might appear in THE_REQUEST is

GET /file.php?id=762ab&sort=pr HTTP/1.1
-or-
HEAD /file.php?id=Ba1g HTTP/1.1

It is exactly the client request header data that appears in quotes in a standard raw server access log file.

Jim

skinter

12:02 am on Oct 25, 2005 (gmt 0)

10+ Year Member



Jd, you are a life saver!

I just have one more request. I have files in a sub-directory, and I wish for them to not be seen naked due to some security problems that might arise. I have another script that reads them exactly how they are intended, but my rewrite code just isn't working. The code, again, is as follows:

RewriteEngine On
RewriteBase /
# Is the next line needed?
RewriteCond %{REQUEST_URI}^/articles/([^&]+)$
RewriteRule ^/dir/([^&]+)$/?ident\=%1 [R=301,L]

I know the syntax is disgraceful, and I know it's not right, but I don't know how else to do it.

And example of this would be /dir/variable (no extension), to /?ident=variable .

Sorry for asking for hand-holding, but I've tried the Apache site, and Google, and barely found anything that I could understand.

jdMorgan

1:51 pm on Oct 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because your example URLs don't match the code you posted, I can't tell what you want to do. You state that you don't want to accept a trailing slash, but your code accepts one, for example. Also, I don't know why you've specified a 301 eternal redirect, since this will expose the script and it's variables to the user. The regular-expressions patterns in your rule, "[^&]+" means, "accept one or more characters not equal to any ampersand," and I doubt that what you want there.

Based soley on this

An example of this would be /dir/variable (no extension), to /?ident=variable

I would think that the simple rule:


RewriteRule ^dir/([^/]+)$ /?ident=$1 [L]

would be a good place to start.

If you do wish to accept a trailing slash, then change the pattern slightly:


RewriteRule ^dir/([^/]+)/?$ /?ident=$1 [L]

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

skinter

9:35 pm on Oct 25, 2005 (gmt 0)

10+ Year Member



Jd, you are amazing! Thank you so much for all that you've contributed.