Forum Moderators: phranque

Message Too Old, No Replies

htaccess conflicting RewriteRules

         

rclemente

7:36 am on Nov 17, 2005 (gmt 0)

10+ Year Member



hi!

i am new to apache htaccess but i was able to code some of it. i am implementing redirecting. my original htaccess is this:

RewriteEngine On
RewriteBase /
RewriteRule ^s(.*)s$ urlscript/gotoURL.php?Surl=$1

What happens here is whenver i access a url in my server [ex. www.webmaster.com/swertus] it would be redirected to the gotoURL.php file under my urlscript folder which is also in the root folder. the htaccess is also in the root folder.

my program is actually a url shortening program, where a long url will be given a short code "s<randomCode>s" then the long url now can be access thru www.myserver.com/s<randomCode>s.

i got this working in 2 servers.
now i am trying to do this in another server which has an existing htaccess in the root folder. since it is not possible to have 2 htaccess in the root folder, i am advice to combine the two.

here is the htaccess in the root folder of the server:

************

RewriteEngine on
RewriteBase /

# subdomain?

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI} ^([^.]+)\.sirlook\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3!^(.*)<>\1$ [NC]
RewriteRule ^(.*)$ - [E=BLOGUSER:%1]

# subdomain / hostheader rewrites

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^page([0-9]+)/?$ blog/index.php?u=%1&page=$1 [L]

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^profile/?$ blog/profile.php?u=%1 [L]

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive_(.*)_(.*)_.html$ blog/archive.php?u=%1&y=$1&m=$2 [L]

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive_(.+).html blog/archive.php?u=%1 [L]

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive.html blog/archive.php?u=%1 [L]

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^([0-9]+)/?$ blog/entry.php?u=%1&e_id=$1 [L]

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^$ blog/index.php?u=%1 [L]

*******

i tried to combine the two htaccess and save it still in the root folder, but whenever i access www.myserver.com/s<randomCode>s i am redirected to the index page of the www.myserver.com!

please help!
additional info:
there is no httpd,conf file in the server so i was also advice to add the line "Options FollowSymLinks"
before the "rewriteEngine on" line.

please please help. i dont really understand why the redirection is freaking out. does the htaccess acknowledge my rewrite rule? or there is a rewrite rull in the htaccess file which redirects my url into the index page

please help.

sorry for the long explanation. just trying to give all the info.

Thanks. :)

jdMorgan

2:26 pm on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rclemente,

Welcome to WebmasterWorld!

I don't see any explicit rule that would cause this 'interference'. But one thing you should consider doing is to 'root' your rewritten URL-paths. For example, instead of using:


RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^page([0-9]+)/?$ blog/index.php?u=%1&page=$1 [L]

use:

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^page([0-9]+)/?$ [b]/b[/b]log/index.php?u=%1&page=$1 [L]

This will prevent the new rule from setting the 'current subdirectory' to "/urlscript" and affecting the other rules if you put the new rule ahead of the others. (It's not clear where you put the new rule in the existing .htaccess file).

Two more tips not related to your question:

1) Avoid the use of ".*" and ".+" sub-patterns in the middle of your patterns. These are ambiguous, and cause the server to have to do a lot of work figuring out which part of the pattern matches which part of the requested URL-path. For example, instead of using:


RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive_(.*)_(.*)_.html$ blog/archive.php?u=%1&y=$1&m=$2 [L]

use:

RewriteCond %{ENV:BLOGUSER} ^(.+)$
RewriteRule ^archive_([^_]+)_([^_]+)_.html$ blog/archive.php?u=%1&y=$1&m=$2 [L]

The use of a "[^_]+" sub-pattern, meaning "match one or more characters not equal to an underscore," allows the rewritengine to immediately know when it has finished matching the first and second back-references, because it knows not to include "_" in either of them. Therefore, it can process the pattern in strict left-to-right order in a single pass, rather than having to make several attempts to match it.

The number of required passes using ambiguous patterns will vary in proportion to the number of characters in the requested URL-path -- obviously not an efficient solution, when a single-pass solution is available.

2) If you get stuck trying to figure this out, you can temporarily change these internal rewrites to external redirects. In this way, you will be able to see the action of your rules by watching your browser's address bar change. Then when you get it working, change the redirects back to internal rewrites so that your internal URL-paths are no longer exposed.

Jim

rclemente

3:41 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



ok, i got your point. but i was not actually the one who coded the existng htaccess. all i want is to just add my htaccess lines to it:

RewriteEngine On
RewriteBase /
RewriteRule ^s(.*)s$ urlscript/gotoURL.php?Surl=$1

these lines are working on 2 servers i tried. but with the current server i am trying to put these codes dont work. htaccess is in the root folder. so when i try to access www.myserver.com/sadfgs iam redirected to urlscript/gotoURL.php?Surl=adfgs

i know it was redirected to this coz the url is exposed in the address browser

however the redirection last for only seconds, it then redirects again to the www.myserver.com which shows its index file.

can you please analyze all the lines and ecplain me why this happens..

thanks a lot for the reply!

jd01

8:41 pm on Nov 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It appears the problem is in your script doing the analyzing of the information passed to it, not in your .htaccess file...

There is no such thing as a 'timed' redirect in Apache, so either you have an enormous load on your server and it is taking a good period of time to redirect you to the home page, or your redirection script is not function properly.

As jdMorgan said, there does not appear to be a reason for the redirect to your home page in the .htaccess file -- I am guessing the variables you are passing to it are not being accepted properly, and that is where your problem is.

Justin