Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite rule help

         

mercnboy3

4:39 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



I would like all requests that aren't files and don't begin with a certain pattern to be redirected.

So:

tld.example.com/dir - would be let through
tld.example.com/css/styles.css - would also be let through

tld.example.com/fake/path - would be redirected

Here is my current setup:


DocumentRoot /var/www
ServerName example.com
ServerAlias *.example.com


RewriteCond %{HTTP_HOST} ^tld.example.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^/dir http://www.example.com [L,R=301]

Currently the above rule is redirecting all non /dir requests

[edited by: mercnboy3 at 4:44 pm (utc) on Mar. 23, 2009]

[edited by: jdMorgan at 5:16 pm (utc) on Mar. 23, 2009]
[edit reason] Please use example.com [/edit]

mercnboy3

4:40 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



oops, I meant to add tld.example.com on the examples

[edited by: jdMorgan at 5:17 pm (utc) on Mar. 23, 2009]
[edit reason] example.com [/edit]

jdMorgan

5:14 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Quick question: Why are you redirecting, instead of returning a 404-Not Found or a 403-Forbidden response?

I'm asking because redirecting all "errors" to your home page is NOT a good idea if you value your search engine rankings...

Jim

mercnboy3

5:25 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



SEO is not a concern for this particular application

jdMorgan

5:55 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{HTTP_HOST} ^tld\.example\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/ http://www.example.com/ [R=301,L]

Be aware that a site with this code should never be taken 'live' if search ranking is a factor -- All non-existent URL requests will be redirected to the home page without further qualification.

Jim

mercnboy3

6:27 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Some of the desired passthru url's (the ones that begin with /dir) are not actual files so I cannot redirect all requests except files or directories as you have done.

g1smd

6:39 pm on Mar 23, 2009 (gmt 0)

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



A RewriteCond can take care of those.

jdMorgan

7:34 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{HTTP_HOST} ^tld\.example\.com [NC]
RewriteCond $1 !^css/
RewriteCond $1 !^dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^/(.*)$ http://www.example.com/ [R=301,L]

Note that according to some benchmarks posted here several years ago by member Andreas Friedrich, separate RewriteConds are faster when the code resides in httpd.conf or another server-config file; In .htaccess, using the local OR ("¦") in a single RewriteCond is faster.

Jim

mercnboy3

7:45 pm on Mar 23, 2009 (gmt 0)

10+ Year Member



Is there any way to do that without having to specify the allowed directories?

thanks

jdMorgan

9:08 pm on Mar 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're the one who must define what is a "fake/path" and what is not -- we don't know what you mean by that, specifically, and the code cannot be guessed at.

What specifically, in the URL, is it that allows you to tell a "fake/path" from a real path. Only by defining that in terms of URL and/or "file exists" can we then tell the server (in the mod_rewrite code) how to tell them apart.

Jim