Forum Moderators: phranque

Message Too Old, No Replies

simple url redirection with .htaccess

         

Saturn

9:51 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



I have
RewriteRule ^errors/(400¦401¦403¦404¦500)/$ index.php?error=$1 [L]

And it works, I am now trying to make a simple redirection so that whoever tries to brows errors/ would be automatically redirected to errors/403/

Suggestions please?

And one more thing.
I asked before about double slash // issue..
I have found a solution but its still a simple problem...

RewriteCond %{REQUEST_URI} ^//+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*/)/+$ [OR]
RewriteCond %{REQUEST_URI} ^/(([^/]+/)*)/+$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)//+(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/([^/]+)//+(.*)$

#RewriteRule / [domain.com...] [R=301,L]

The above works with every // but the very first one under one condition.
Means
[site.name...] <---
only when double slash before and nothing after, I can't handle it.
Can you help me fix this please?

Regards,

[edited by: Saturn at 10:29 pm (utc) on Aug. 15, 2007]

jdMorgan

2:48 am on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I assume that you have defined your error-handing documents like this:

ErrorDocument 403 /errors/403/
ErrorDocument 404 /errors/404/

If so, you can use THE_REQUEST to differentiate client requests from internally-generated or internally-rewritten requests:

# Rewrite error pages to index.php with error code in query string
RewriteRule ^errors/(400¦401¦403¦404¦500)/$ index.php?error=$1 [L]
#
# Prevent direct client access to error pages (except for 403)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /errors/
RewriteRule %{REQUEST_URI} !^/errors/403/
RewriteRule ^errors/ - [F]
#
# Prevent direct client access to error-handling script
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?error=
RewriteRule ^index\.php$ - [F]

There's no way to stop the client from directly-requesting /errors/403/ that won't cause an infinite 403 error loop when any such access is attempted, so the code above explicitly allows it.

To clarify, a typical value of THE_REQUEST would look like this:

GET /index.php?page=1&sort=ascending HTTP/1.1
(This example was chosen to show all the "parts" -- not because the URL is in any way relevant to your site.)

Be aware that you could eliminate the need for the first two rules by defining your error documents like this:


ErrorDocument 403 /index.php?error=403
ErrorDocument 404 /index.php?error=404

You would then need to change the third (remaining) rule above to prevent the previously-mentioned loop:

# Prevent direct client access to error-handling script
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?error=
RewriteCond %{QUERY_STRING} !^error=403$
RewriteRule ^index\.php$ - [F]

I'd like to state that I believe it's best practice to avoid using scripting languages for any critical errors such as 403 and 500 errors. That's because if a script or a script interpreter causes one of these errors and is also used to handle that error, then you could end up wrapped around the axle, and unable to log, report, or find the cause.

To avoid this, critical error handlers should be plain, static-HTML documents with no external dependencies -- No images, no external scripts, no external CSS -- completely self-contained. And to those who say that they want all their pages to "fit their template" and to "match their site's 'look and feel'", I say, "It's an error page! It'll stink when anyone sees it, no matter how pretty it is!" Or, as someone else posted here very recently, "You can't put lipstick on a pig!" ;)

For a redirect to fix double slashes, see: [webmasterworld.com...]

Jim

Saturn

7:31 am on Aug 16, 2007 (gmt 0)

10+ Year Member



Hi Jim,

I have followed your instructions as follows.

ErrorDocument 400 /errors/400/
ErrorDocument 401 /errors/401/
ErrorDocument 403 /errors/403/
ErrorDocument 404 /errors/404/
ErrorDocument 500 /errors/500/

# Rewrite error pages to index.php with error code in query string
RewriteRule ^errors/(400¦401¦403¦404¦500)/$ index.php?mIRCdotOrgPageID=$1 [L]

# Prevent direct client access to error pages (except for 403)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /errors/
RewriteRule %{REQUEST_URI}!^/errors/403/
RewriteRule ^errors/ - [F]
#
# Prevent direct client access to error-handling script
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?error=
RewriteRule ^index\.php$ - [F]

But still, no redirecting at all. I am testing locally, when I try [localhost...] I get no redirecting and I also get the default apache error page, I wonder why I am not getting the custom page (could be xampp) I am not sure.

Note: I assume you are aware I do NOT have a folder named errors, I am using mod_rewrite.

---
Forbidden

You don't have permission to access /site/errors/ on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
Apache/2.2.0 (Win32) DAV/2 mod_ssl/2.2.0 OpenSSL/0.9.8a mod_autoindex_color PHP/5.1.1 Server at localhost Port 80
---

Saturn

11:04 am on Aug 16, 2007 (gmt 0)

10+ Year Member



Quote: Jim
-----------------------------------------------------------------------
For a redirect to fix double slashes, see: [webmasterworld.com...]

That didn't completely fix my issue [localhost...] <-- note the two slashes?
I can still use as many slashes as I want starting the string...

jdMorgan

5:19 pm on Aug 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Forbidden

You don't have permission to access /site/errors/ on this server.

You'll need to fix that first, before worrying about multiple slashes. Keep your code simple, get it working one small step at a time, and then add the 'fancy decorations' later.

Comment out the entire ruleset that forbids access to your index.php?error=4xx URLs, flush your cache, test, and let us know the result.

Jim

Saturn

8:28 pm on Aug 16, 2007 (gmt 0)

10+ Year Member



It worked now. After I flushed my cache and corrected my ErrorDocument rule.
Thanks a million :)