Forum Moderators: phranque

Message Too Old, No Replies

htaccess question please

htaccess redirect question

         

auxiv

3:10 am on Sep 5, 2009 (gmt 0)

10+ Year Member



Hello!

I am have problems redirecting with htaccess. Im sure this will be easy for some here, however Its been killing me for hours now.
Here's the rule:


RewriteRule ^demo/(.*)$ /auxdir/models/$1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /auxdir/models/(.*)$\ HTTP/
RewriteRule ^auxdir/models/(.*)$ http://www.example.com/demo/%1 [R=301,L]

(i have also tried different variations of above code)
such as

RewriteRule ^demo/(.*)$ /auxdir/models/$1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /auxdir/models/\ HTTP/
RewriteRule ^auxdir/models/ http://www.example.com/demo/? [R=301,L]

I would like the request for /auxdir/models/ to redirect to /demo/ (dir 'demo' and contents)

The code I am using doesnt seem to work.
very frustrating.
Thanks in advance!

[edited by: jdMorgan at 1:04 pm (utc) on Sep. 5, 2009]
[edit reason] example.com [/edit]

g1smd

12:38 pm on Sep 5, 2009 (gmt 0)

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



You have a rewrite placed before your redirect
in both example code blocks, and that will cause problems.

Always list rewrites AFTER redirects.

Add a RewriteCond to your rewrite so that it only fires for Direct Client Requests. Add the [L] flag to the rewrite.

jdMorgan

1:14 pm on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The rule order is OK in this case, since the redirect rules check THE_REQUEST.
Performance will be better with [L] on the internal rewrites, but this error is non-fatal as well (for the same reason).

So...

Do you have any working rewriterule redirects in this .htaccess file?

Will something simple like the following rule work?


RewriteRule ^foo\.html$ http://www.WebmasterWorld.com/ [R=302,L]

If not, then there's likely a server configuration problem (or the required mod_rewrite 'setup' directives are missing from your code)

When posting here, please understand that "it doesn't work" is not a very useful statement. For best results, we need to know:

What URL did you type?
What did you expect to happen?
What in fact did happen?
How did the actual results differ from your expectations?
Are there any relevant entries in your server error log?
Did you delete your browser cache before testing?

Jim

auxiv

2:13 pm on Sep 5, 2009 (gmt 0)

10+ Year Member



Thank you both for your response(s).

The root http directories htaccess file is quite extensive. I utilize htaccess lots (so could be something conflicting)
htaccess works great thus far. I have directory "models" that sits in "auxdir" directory. The "auxdir" directory sits in root http directory. (/www/auxdir/models/) I want to rename the directory "models" to "demo" and eliminate "audir". would be "/www/demo/" after htaccess or "example.com/demo/". The folder "demo" (really '/auxdir/model/') has lots of php files and directories all corresponding to an index.php file "example.com/demo/index.php?uid_pid=weeee-etc-etc".

The Rule:

RewriteRule ^demo/(.*)$ /auxdir/models/$1 

Does work correctly. I am able to access-->

"example.com/demo/index.php?etc-etc"...

but will not redirect when the real path is requested.

"example.com/auxdir/models/index.php?etc-etc"

i have been trying to get a redirect in htaccess to work for this auxdir/models directory I have done it this way for many other directories, yet have troubles with only this one. I realize my rules may not be specific enough. I need to have more specific rules for this one directory.

jdMorgan

2:50 pm on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Reverse the rules as recommended above as a best-practice, even though it won't make a difference here. Remove the "$" from the redirect rule's RewriteCond pattern, and change that pattern to a more specific "match all until space found" pattern.

Then, in the substitution URL, you *can* back-reference the RewriteRule's pattern match instead of the RewriteCond's pattern-match to simplify query-string handling (the query will pass through unchanged by default). I show both methods below, with differences highlighted.

So, the rules should look like this:


# Externally redirect (only) direct client requests for /auxdir/models/ URLs to /demo/ URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /auxdir/models/[b][^\ ]*[/b]\ HTTP/
RewriteRule ^auxdir/models[b]/(.*)$[/b] http://www.example.com/demo[b]/$1[/b] [R=301,L]
#
# Internally rewrite /demo/ URL requests to /auxdir/models/ directory
RewriteRule ^demo/(.*)$ /auxdir/models/$1 [L]

-or like this-

# Externally redirect (only) direct client requests for /auxdir/models/ URLs to /demo/ URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /auxdir/models/[b]([^\ ]*)[/b]\ HTTP/
RewriteRule ^auxdir/model[b]s/[/b] http://www.example.com/demo[b]/%1[/b] [R=301,L]
#
# Internally rewrite /demo/ URL requests to /auxdir/models/ directory
RewriteRule ^demo/(.*)$ /auxdir/models/$1 [L]

Delete your browser cache before testing...

Jim

[edited by: jdMorgan at 2:51 pm (utc) on Sep. 5, 2009]

auxiv

2:51 pm on Sep 5, 2009 (gmt 0)

10+ Year Member



What URL did you type? --- A. example.com/auxdir/models/
What did you expect to happen? --- A. redirect to example.com/demo/
What in fact did happen? ----A. absolutely nothing (page loaded with no redirect to /demo)
How did the actual results differ from your expectations? A. -- It didnt redirect =(
Are there any relevant entries in your server error log? A. -- No errors
Did you delete your browser cache before testing? A. -- yes.

(thank you)

jdMorgan

2:52 pm on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... cross-posted. (See above) :)

auxiv

3:17 pm on Sep 5, 2009 (gmt 0)

10+ Year Member



I know your code is correct. yet still will not redirect.
I searched the htaccess file for any other entries pertaining to the /auxdir/models/ dir and there are none.

Cleaned cache on 2 separate pcs, still will not redirect for some strange reason. I wrote some php code to try to accomplish this as well, using an if statement. I basically told php to look out for "auxdir/models/" in the requested URL, and when true to redirect... for some strange reason this triggered an infinite loop, even when "auxdir/models/" was not in the URL. (requesting just '/demo/') <--see no "auxdir/models/" (yet still looped in php)

(T.Y. fyh)

auxiv

3:33 pm on Sep 5, 2009 (gmt 0)

10+ Year Member



theres and .htaccess inside my "/auxdir/models/" directory.

includes:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /auxdir/models/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /demo/index.php [L]
</IfModule>

Although my application does not function correctly without this htaccess file, I temporarily removed it to see if it would then redirect, and a saw no difference.

Thanks again for your time and help.

jdMorgan

3:40 pm on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your PHP solution looked at REQUEST_URI, but not at THE_REQUEST. If you don't look at THE_REQUEST, you cannot tell a client request for a URL of /auxdir/models/* from an internally-rewritten request for a filepath of /auxdir/models/*
As a result, the PHP code and the internal-rewrite rule together constitute an infinite loop.
I don't know that PHP *can* examine THE_REQUEST; It may be a variable defined in mod_rewrite only. (I'm not sure, but if PHP always sees it as blank or as "(None)", then don't waste too much time on it.)

Things to check:

  • Is the .htaccess code to redirect client requests for /auxdir/models/* located along the filepath to which requests for that URL will resolve?
  • Assuming you don't use them, have you disabled the MultiViews Option (content-negotiation)?
  • Again assuming you don't use it, have you disabled AcceptPathInfo (Apache core 2.x and above only)
  • Do you have any Redirect, RedirectMatch, Alias, or ScriptAlias directives in the server config which would match this /auxdir/models/* URL-path?
Jim

auxiv

4:11 pm on Sep 5, 2009 (gmt 0)

10+ Year Member



thanks will check everything again.

jdMorgan

4:41 pm on Sep 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the /auxdir/models/.htaccess, you should not need the <IfModule> conatiner. Nor should you need the RewritBase directive, unless this directory is Aliased in your server config file.

You may also wish to try adding a RewriteOptions inherit directive above the code that's left in there.

Jim