Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
July 31, 2014, 05:45:09 PM

Pages: [1]   Go Down
  Print  
Author Topic: .htaccess Redirection  (Read 2702 times)
Lachlan McDonald
Newbie
*
Offline Offline

Posts: 5


WWW
« on: January 18, 2009, 01:58:55 AM »

Evening lads,

I'm trying to do a .htaccess redirection were all attempts to load a directory which does not exist will be redirected to a PHP script. When a directory does exist, no redirection will occur. However, when I include the code below in public_html/.htaccess file, I get HTTP 500 error.

Code:
RewriteCond %{REQUEST_URI} !-d   [NC]
RewriteRule ^/(.*) /inc/test.php?id=$1    [NC,NE]

I've also tried to spell out the directories instead as shown below:

Code:
RewriteCond %{REQUEST_URI} !^/(img|imgs|about|contact|support)/    [NC]
RewriteRule ^/(.*) /inc/test.php?id=$1    [NC,NE]

But I still get a 500 error. It seems no matter what I write, if I try to negate the operation with a exclamation mark, I get thrown an error.

Any idea how to overcome this?

Cheers,

Lachlan
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5855



« Reply #1 on: January 18, 2009, 12:09:39 PM »

A couple of things to try:

1) use %{REQUEST_FILENAME} instead of %{REQUEST_URI}

2) the leading "/" is stripped off for you:  RewriteRule  ^(.*)$ ....
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
Lachlan McDonald
Newbie
*
Offline Offline

Posts: 5


WWW
« Reply #2 on: January 18, 2009, 08:04:43 PM »

Thanks MrPhil, but unfortunately I'm still getting the 500 error.

Code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /inc/test.php?id=$1    [NE]
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5855



« Reply #3 on: January 19, 2009, 04:55:00 PM »

Have you tried it without the NE flag? What are you trying to keep from being escaped?
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
Lachlan McDonald
Newbie
*
Offline Offline

Posts: 5


WWW
« Reply #4 on: January 19, 2009, 07:38:30 PM »

Ah, I didn't realise I left that in. I removed it, but I'm still getting the Internal Server Error. It seems if I remove the exclamation mark, Apache will match existing directories. So it seems trying to negate the expression with the exclamation mark is what is causing the error.
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5855



« Reply #5 on: January 19, 2009, 07:57:18 PM »

I don't know. You're using the standard ASCII or Latin-1 character set's exclamation point, and not something exotic? Unless someone else can spot something, all I can suggest is opening a support ticket. Sometimes there are server misconfigurations that only LP support can fix, that cause inexplicable internal server errors.
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
Lachlan McDonald
Newbie
*
Offline Offline

Posts: 5


WWW
« Reply #6 on: January 19, 2009, 10:10:57 PM »

That's what I thought too, but I double checked everything and it all checked out. Looks like I will have to open a support ticket.
Logged
Lachlan McDonald
Newbie
*
Offline Offline

Posts: 5


WWW
« Reply #7 on: January 26, 2009, 05:30:38 PM »

Whilst creating a test directory to show the error to the support staff, I discovered that the following works:

Code:
RewriteCond   %{REQUEST_URI}                           ^/[a-z][a-z0-9]+/$
RewriteCond   /home/radel3/public_html/%{REQUEST_URI}  !-d
RewriteRule   ^(.*)                                    profiles.php?id=$1&fn=%{REQUEST_URI} [L]

It seems that the the moment I use an exclamation mark and a forward-slash in the directive, things go bad. So long as the script I attempt to load is in the base directory, everything works as expected.
« Last Edit: January 26, 2009, 05:56:45 PM by Lachlan McDonald » Logged
daniels0xff
Newbie
*
Offline Offline

Posts: 1


« Reply #8 on: February 19, 2009, 11:17:57 AM »

Hi,

Try this.
It checks if the file/folder exists and if not it rewrites to script.php?id=

Code:
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /script.php?id=$1 [L]
</IfModule>
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: