Forum Moderators: phranque

Message Too Old, No Replies

URL Subdomain rewriting and directory exclusion

relative links for images, styles and scripts won't work

         

gaetanb

1:28 pm on Jul 19, 2010 (gmt 0)

10+ Year Member



Hello everyone,

I am rewriting my URL with htaccess and successfully rewrote a subdomain from admin.website.com to www.website.com/admin.

However, I now have troubles since my images, css and scripts are being rewritten to admin.website.com/script/...

My directory structure from the root is:

/admin
/images
/style
/scripts

Can someone help me to exclude several directories from rewriting (images, scripts and style) ?

Please note that I already tried using rewriting rules from this forum but it didn't work. It might be a problem of order in the rules and conditions.

Hereunder, my htaccess code:

Options -Indexes +FollowSymlinks

RewriteEngine on

RewriteBase /

#Subdomain
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^admin\.website\.com$ [NC]

RewriteRule ^(images|scripts)$ - [L]
RewriteRule ^/(.*)$ http://admin.website.com/$1

#ROOT
RewriteRule ^([a-z]+)$ index.php?lang=$1 [QSA,L]
RewriteRule ^([a-z]+)/$ index.php?lang=$1 [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)$ index.php?lang=$1&section=$2&categorie=home [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?lang=$1&section=$2&categorie=$3 [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?lang=$1&section=$2&categorie=$3&page=$4 [QSA,L]


I hope I was clear enough.

Thank you in advance for your help.

Gaetan

jdMorgan

2:39 pm on Jul 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your exclusion needs to go at the top, not in the middle of another rule's RewriteConds...

RewriteEngine on
#
RewriteBase /
#
# Skip all remaining rules if the request is for /images or /scripts or /styles
RewriteRule ^(images|scripts|styles)$ - [L]
#
#Subdomain
RewriteCond %{HTTP_HOST} !^(admin\.example\.com)?$ [NC]
RewriteRule ^(.*)$ http://admin.example.com/$1 [R=301,L]
#
#ROOT
RewriteRule ^([a-z]+)/?$ index.php?lang=$1 [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)$ index.php?lang=$1&section=$2&categorie=home [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?lang=$1&section=$2&categorie=$3 [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?lang=$1&section=$2&categorie=$3&page=$4 [QSA,L]

Note that there are many other corrections and modifications for efficiency.

The "admin" redirect code still does not look quite right, as I would expect the RewriteRule pattern to be looking for "/admin" in the path unless this code is located in /admin/.htaccess ...

Jim

[edited by: jdMorgan at 3:12 pm (utc) on Jul 19, 2010]

gaetanb

2:59 pm on Jul 19, 2010 (gmt 0)

10+ Year Member



Thank you for your help Jim.
However, I now get a 500 Internal Error with the line

RewriteCond ^(images|scripts|styles)$ - [L]

Is there a mistake somewhere?
I have to mention this htaccess is at the root of my website, I don't have any htaccess in my admin. If I need some, what should I write inside?

Thanks a lot,

jdMorgan

3:13 pm on Jul 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is there a mistake somewhere?

Yes. It should have been "RewriteRule."

Please see the corrected code above.

Jim

gaetanb

3:36 pm on Jul 19, 2010 (gmt 0)

10+ Year Member



Okay now, no more Error 500 but images in admin.example.com that are written in relative path: src=../images/img.gif are still rewritten admin.example.com/images/img.gif

I don't know what to try anymore ...

g1smd

4:47 pm on Jul 19, 2010 (gmt 0)

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



Don't use relative URLs.

Start the link with a leading slash, and specify the filepath to the file too.

URLs are resolved by the browser.

gaetanb

7:50 am on Jul 20, 2010 (gmt 0)

10+ Year Member



Sorry but the following code will still not display images with src="/images" contained in /images directory in root foler. My admin foler is contained in /admin directory in root foler as well.

RewriteRule ^(images|scripts|style)$ - [L]

RewriteCond %{HTTP_HOST} ^admin\.website\.com
RewriteRule ^/([a-z0-9]+)$ http://www.website.com/admin$1 [R=301,L]



Actually, after test, my images under path src="/images" will display only if located under /admin/images
which is why I think the redirect script has to be modified.


Any other clue?

g1smd

10:33 pm on Jul 20, 2010 (gmt 0)

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




The redirect code cannot work as presently written because including the leading slash in the pattern is an error.

jdMorgan

5:44 am on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In addition to that, we need correct and complete information to help solve the problem.

Is this code in the root .htaccess file?

What is the image URL being requested? (Use the Live HTTP Headers add-on for Firefox --or a similar tool-- to find out).
What the filepath being reported in your server Error log when you get an image-loading error?
What should the (correct) image URL be?
What should the correct image filepath be?

Basically, when you report an error, please tell us all of those things -- How you tested, what were the expected results, what were the actual results, and how did the actual results differ from the expected results -- Please remember that we do not know anything about your site, and we cannot see your monitor... :)

Jim

gaetanb

7:29 am on Jul 23, 2010 (gmt 0)

10+ Year Member



Hello Jim!

Thank you for your reply.
I actually created my subdomain through my web host's apache server redirecting from admin.domain.com to www.domain.com/admin.

The .htaccess in root and I suspect images don't work because when I call img src="/images/img.jpg" or img src="../images/img.jpg" it does not link me to www.subdomain/admin/images/img.jpg (my original image folder) but it searches in www.subdomain.com/admin/images/img.jpg.

In other words, when I am on my subdomain, the root was changed from www.domain.com to www.domain.com/admin.
Therefore, relative paths will only work when I put a sub-folder inside the /admin folder.

I hope this is clearer. I would appreciate a solution for jpg, jpeg, png, gif, css and js files.

My root:

/admin --> index.php calls image, js and css URLs
/images --> jpg, jpeg, png, gif
/style --> css
/scripts --> js

Thank you in advance,

jdMorgan

5:50 pm on Jul 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I actually created my subdomain through my web host's apache server redirecting from admin.domain.com to www.domain.com/admin.

If this is the case, then it is possible that the .htaccess code in root won't even be executed for calls to the 'admin' subdomain. Change one image link on an admin page to server-relative (path starts with slash), rename your .htaccess file(s) to "disable" it/them temporarily, and then test that image link. If it does not work, then the problem is in your server configuration, and cannot be 'fixed' by .htaccess.

Jim

gaetanb

9:07 pm on Jul 23, 2010 (gmt 0)

10+ Year Member



You are right, htaccess won't solve it. I have to check with server configuration and contacted my web host.

Thank you for your help though.