Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite massive headaches!

         

jimmyslam

12:09 pm on Apr 23, 2010 (gmt 0)

10+ Year Member



We are triying to put a website subdomain url to redirect to an specific file (viewer.php) sending the value of this subdomain: viewer.php?user=%1

So we got it working but now everytime that a external file is requested from that first file(viewer.php) it gets redirected to itself. So all assets will have the content of viewer.php

We started adding exceptions but we could end up having a massive list, is that supposed to be like this?


# switch on mod_rewrite ##
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

# For non-www subdomain requests, the query string parameter 'user' is taken from the requested subdomain (%1)
rewriteCond %{HTTP_HOST} ^([^.]+)\.superFolder\.[a-z]{2,3}
rewriteCond %{REQUEST_FILENAME} !^(.+)\.(css|js|swf)$
rewriteRule ^([^/.]*) superFolder/HTML/viewer.php?user=%1&urlb=$2 [QSA,L]

# if the request_uri has more than "/" and is not from the /HTML directory don't redirect
rewriteCond %{REQUEST_URI} ^/(.+)$
rewriteCond %{REQUEST_URI} !^(.+)/HTML/(.+)$
rewriteRule ^(.*)$ superFolder/HTML/%{REQUEST_URI} [L]


Help would be greatly appreciated.

Thanks!

jdMorgan

12:54 pm on Apr 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is the most important part of the solution, and the question requiring an answer in order for anyone here to be able to help you:

> every time that an external file is requested from that first file (viewer.php)...

What is the precise and complete definition of "an external file"?

There are several problems with the code above -- undefined back-references, recursion, logic errors, inefficiencies, and a failure to do what the comments say it should do. But...

Without a solid definition of what URLs (not files) you wish to rewrite (not redirect), it's fairly impossible to make useful recommendations.

Jim

jimmyslam

1:52 pm on Apr 24, 2010 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for replying,. Sorry for not talking more about external files!

When I say it, I mean javascript, css, flash, etc. Basically all items that get loaded from the HTML:
for example:

<link href="css/style.css" rel="stylesheet" type="text/css"/>

or
<script type="text/javascript" src="js/swfobject.js"></script>

these files reside in:
in
superFolder/HTML/css/
superFolder/HTML/js/

We have flash files requesting files from:
"superFolder/Resources/otherfolders/" and other folders that are contained in "superFolder/"

I hope that can guide you to help us. :)

In any case thank you very much for showing interest, we have been stuck for days on this matter!

jdMorgan

3:40 pm on Apr 24, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Rather than "getting too specific" by excluding particular directories and thus creating a maintenance nightmare and constraining future file additions to only those directory-paths, I'd suggest using only the first rule you posted and excluding by URL-extension, as already attempted in your code.

With a few minor tweaks, all that should be required is something like this:

# enable mod_rewrite
Options +FollowSymLinks
#
# enable the rewriting engine
RewriteEngine on
#
# reset rewritebase to default
RewriteBase /
#
# For non-www subdomain requests of top-level extensionless URL-paths, invoke the "viewer.php"
# script, passing it the requested subdomain and URL-path as query string parameters
RewriteCond %{HTTP_HOST} !^www\.example\.[a-z]{2,3}
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.[a-z]{2,3}\.?(:[0-9]+)?$
RewriteRule ^([^/.]*)$ superFolder/HTML/viewer.php?user=%1&urlb=$2 [QSA,L]

The primary and most important "fix" here is the "$" end-anchor on the RewriteRule pattern. This is critical to the pattern's function of rejecting URL-paths containing additional slashes or any periods.

When defining requirements for rules, you must base such exclusions primarily on characteristics of the client's HTTP request, since the RewriteRule directive itself tests the client-requested URL-path -- not "files" or anything else. Additional conditions may be added using rewriteconds, but the RewriteRule directive tests only the requested URL-path.

Therefore, define both what should and what should not be rewritten or redirected by making comprehensive lists of URL-paths or URL path-part descriptions. An example list for the rule above would be:
Requirements:

Do rewrite URL-paths matching "/<any-path-not-containing-slashes-or-periods>"
Do not rewrite URL-paths matching "/<any-path-which-contains-slashes-or-periods>"

I show these URL-paths as "descriptions" enclosed in "<>" here, because they are not literal strings -- This in contrast to more-typical descriptions which would often contain specific literal URL-paths or path-parts.

So for best results, think in terms of client-requested URL-paths when writing requirements for rules and the rules themselves.

Also, concise but comprehensive and strictly-accurate comments will save you a lot of time and headaches in the future; In fact, if my code comments or requirements description above don't sound exactly right, then the code I posted is guaranteed to be incorrect. :)

Jim

jimmyslam

12:10 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Thank you very much,

There is still problems, i believe there is a still missing rule?:

So there are some requests from the flash file that try to access "../otherFolders/".
When I say otherFolders it means any folders/ files.

Before, it was completly fine because all requests started from "domain/superfolder/HTML/" and I just had to go one level up to make all requests done properly. But now, after the mod_rewrite, the client thinks that its on root or the domain and browser converts all paths into "domain/otherFolders/". Making it not able to find any files, as they are not there. (they are in domain/superFolder/otherFolders/)

The problem with this, at the moment, is that htacess detects it as a path doesnt rewrite the URLs.

So I thought of adding two lines after the first condition, but i am unsure what to write, but I tried for a while but it didn't really worked:

rewriteCond %{REQUEST_URI} ^(.*)$
rewriteRule ^([^/.]*) /superFolder/HTML%{REQUEST_URI} [QSA,L]

All I know that %{HTTP_URI} always starts with "/" and it will have more data if there is some kind of path there. So If that happens, it should start from there: [i]/superFolder/HTML/%{REQUEST_URI}[/i].


Do you think that you can spot the problem?

Thanks a lot for helping us out. This is just like VooDoo, my friend said. We I think we found a chief! :)

[edited by: engine at 2:08 pm (utc) on Apr 28, 2010]
[edit reason] fixed typo [/edit]

jdMorgan

2:20 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Attempts to "rewrite" these requests will result in duplicate content -- the same content appearing at more than one URL.

The proper solution is to use server-relative or canonical links on your pages, instead of trying to use page-relative links.

So, for a simple example of included image objects, use <img src="/images/logo.gif"> or <img src="http://example.com/images/logo.gif"> and not <img src="images/logo.gif"> or <img src="../images/logo.gif">

The same would apply to includes of CSS files, JavaScript files, and <object>s and <embed>s for media files.

It is the browser that resolves page-relative links in the HTML --in accordance with the "directory-path" that it "sees" in its address bar-- in order to determine the full URL that it needs in order to send a request for the included object to your server.

Jim

jimmyslam

3:58 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



That is how I am doing it already on my webserver. But I cant really do it from the flash file. So I have decided to redirect everything except if (URI requests start with /superFolder/)

Im triying this code as well... and no luck!

Gives error 500:
RewriteCond (%{REQUEST_URI}) ^.*$
RewriteRule ^.*$ superFolder/HTML/$0 [QSA,L]


The most weird thing is that I can send the $0 variable to a redirected file:

Redirects and value %0 is correct.
RewriteCond (%{REQUEST_URI}) ^.*$
RewriteRule ^.*$ superFolder/HTML/viewer.php?urlb=$0 [QSA,L]
And it works!....

But I need first one to work, not this one...

If I get this right I will be able to change all my PHP code and flash will be working nicely!


So, after this, the headache is still there and this codes i places, doesn't follow any logic. Is here something that I am missing?

jimmyslam

9:32 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Im still triying to make this work. :'(
I have spent so many hours!

So this code:
[b]RewriteRule ^.*$ http:// %{SERVER_NAME}/superFolder/HTML%{REQUEST_URI} [QSA,R,L][/b] 


This one repeats a kind of loop that keeps adding /superFolder/HTML:

When try to load:
http://www.domainName.com/preLoader.swf


but then it gets to be:
http://www.domainName.com/superFolder/HTML/preLoader.swf


which is what I want! but the MOD_REWRITE detects again it and converts it into:
http://www.domainName.com/superFolder/HTML/superFolder/HTML/preLoader.swf


which gets converted into:
http://www.domainName.com/superFolder/HTML/superFolder/HTML/superFolder/HTML/preLoader.swf


and so on... how can I stop this loop?

<snip>

[edited by: jdMorgan at 11:12 pm (utc) on Apr 28, 2010]
[edit reason] TOS #20 [/edit]

g1smd

9:43 pm on Apr 28, 2010 (gmt 0)

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



The first request matches the pattern and is redirected.

The second request arrives at the server, and the pattern (.*) matches all requests, so the user is redirected again.

Solution: make your pattern more selective.

Perhaps you could add a negative match RewriteCond such that the redirect will happen only if REQUEST_URI does NOT begin ^superFolder/ so you'll use ! for the NOT operator here.

Be aware your current code returns a 302 redirect.

jimmyslam

9:56 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



Yeah, what I could do is to detect if there is not "superFolder/" on the URL.

How do I do that? My brain has been consumed after about 8 hours trying different combinations.

g1smd

10:23 pm on Apr 28, 2010 (gmt 0)

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



Add a preceding negative match RewriteCond such that the redirect will happen only if %{REQUEST_URI} does NOT begin ^superFolder/ so you'll use ! for the NOT operator here.

jdMorgan

11:09 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> How do I do that? My brain has been consumed after about 8 hours trying different combinations.

If after many hours something still will not work, then try something else...

I'll wager the that it would take less than half of 8 hours to thoroughly study the resources cited in our Apache Forum Charter [webmasterworld.com] and thereby become much less dependent on forum responses and guesswork (which almost surely will not be successful).

The forum Charter also lays out our policies here, and will explain why you're getting half-answers to questions about a trivial snippet of code...

Please accept this advice as well-intentioned; It does you no good --and actually can be a huge risk-- to put configuration code on your server if you do not fully-understand it, both from the standpoint of how it rewrites or redirects URLs, and from the standpoint of how its function will affect what search engines see when they request those URLs and whether or not they will "like it" in terms of how those URLs are ranked.

Although this will end up being only two to four lines of code --something easily dismissed as unimportant-- be aware that one little typo or a tiny logic error in your code can ruin your search rankings overnight and put you out of business.

See that subtle mention of your code doing a 302 redirect in g1smd's post above? A 302 in the wrong circumstance can be quite deadly to a URL's listing in search engines, and this is indeed "the wrong circumstance." It's likely you don't want a redirect at all, but rather an internal rewrite to add that folder path into the filepath you use to access the content.

Please post your best-effort at adding a negative match RewriteCond to your rule as a basis for continued discussion.

Thanks,
Jim