Forum Moderators: phranque

Message Too Old, No Replies

htaccess help

         

StevenSykora

6:39 am on Dec 23, 2008 (gmt 0)

10+ Year Member



I have the following htaccess file


# This code handles redirecting to user profiles
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ [DOMAIN.com...] [R]

# This code handles fixing the log in problem wih vBadvanced
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^DOMAIN\.com
RewriteRule ^(.*)$ [DOMAIN.com...] [R=permanent,L]

As the above states, I have two cases.

One to redirect domain.com/WHATEVER_USERNAME_IS_REQUESTED to domain.com/vb/member.php?username=WHATEVER_USERNAME_IS_REQUESTED

The other is to correct some cookie issues that I believe is unrelated to my problem but I thought I should include it in this post so you can see the entire file.

I want to exclude files and folders that actually exist on my server from being re-written URL's that direct you to a user profile. For instance, the folder blogs exists on my server and if I type in [DOMAIN.com...] I do not want to be redirected to [DOMAIN.com ].

After reading up on htaccess files, I had thought that the first two RewriteCond lines in the file would solve this problem, but for some reason when I type in [DOMAIN.com ] my browser gives me the following error.

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

I'd appreciate any help or pointers anyone here would be willing to give me.

Best regards.

jdMorgan

4:10 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that your first rule redirects to example.com, while your second rule redirects to www.example.com. So you've set up a redirection loop if the file does not physically exist, and the server is doing exactly what you told it to do...

Note also that the first rule causes a 302 redirect, not a 301. 302 redirects can cause great problems with search engine listings and rankings of your site's URLs, and you should use R=301 or R=Permanent in your redirects unless you specifically want to mark the redirect as temporary.

In fact, there is no reason to redirect at all, and doing so 'exposes' your "member.php" script URL and query strings in the browser address bar. You'll likely be much happier if you internally rewrite those URLs instead of redirecting them:


Options +FollowSymLinks
RewriteEngine on
#
# Externally redirect to canonical hostname to fix vBadvanced login
# problems and to prevent duplicate content problems in search engines
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite all requests for URL-paths which do not resolve
# to existing files or directories to user profile script
RewriteCond $1 !^vb/member\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /vb/member.php?username=$1 [L]

Note that redundant directives have been removed, an exclusion has been added for efficiency, and that the rule order must now be reversed -- External redirects first, internal rewrites second.

Jim

[edited by: jdMorgan at 4:36 pm (utc) on Dec. 23, 2008]

StevenSykora

4:33 pm on Dec 23, 2008 (gmt 0)

10+ Year Member



You are AWESOME. Thank you VERY much! :)

jdMorgan

4:36 pm on Dec 23, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nah, I typo'ed the post above. Note that [R] in the last rule must be changed to [L] as shown in the corrected post.

Jim

StevenSykora

3:07 am on Dec 24, 2008 (gmt 0)

10+ Year Member



Too easy! Thanks again for the help! :)

StevenSykora

6:09 am on Dec 29, 2008 (gmt 0)

10+ Year Member



I ended up having to change the [L] back to the [R] because some of the images used on member profile pages weren't being parsed in the root directory because they were being called by relative paths instead of their full paths. Do you recommend going in and finding all of the relative path's and hard coding them with the full paths? If so, why? Is just redirecting to the user profile somehow going to create problems for me instead of rewriting actual URL's? Thanks again for all the help! :)

jdMorgan

4:34 pm on Dec 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, fix all the relative paths instead of adding [R]. If you [R]edirect, then the " /vb/member.php?username=$1" URL is exposed to the browsers and search robots, and this rather defeats the whole purpose.

This problem is due to the fact that is is the client --browser or robot-- which resolves relative links. It does so based on the directory-level that is "sees" in its address bar. With your new URLs, the client is looking for relatively-linked images in or below the "/WHATEVER_USERNAME_IS_REQUESTED" directory level.

Use server-relative or canonical URLs in your links, as in <img src="/images/logo.gif"> or <img src="http://www.example.com/images/logo.gif"> to avoid this problem.

Jim

StevenSykora

3:46 am on Dec 30, 2008 (gmt 0)

10+ Year Member



How do I hardcode links that use variables though? i.e $userinfo[profilepic] etc?

jdMorgan

4:48 am on Dec 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, the answer rather depends on how you generate links now... It could be "just stick a literal slash character on the front with PHP," or it could be "change the database URL-path entry or retrieval routine to force URL-paths to be calculated from the Web-root directory."

This is not an Apache-specific question and I don't know your site or your code, so those are just my best guesses.

Jim