Link to home
Start Free TrialLog in
Avatar of lucidica
lucidicaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Setting the mod rewrite path in .htaccess

Hello

I've set up some hosting for a client of mine and he's currently trying to test a site created with a CMS before making it live (changing DNS).

To test sites, the hosting company I use give you [server_IP]/[site_name].  However, this messes up mod rewrite.  

Looking at the site on the temp address breaks, as all of the absolute URLs include public_html (this is based on the paths to the script, which is correct on the server), but the temp URL automatically puts you into public_html.

For instance, the path to the styles in the html:
http://[server_IP]/[site_name]/public_html/assets/css/style.css

However, to get to the styles you actually need this URL:
http://[server_IP]/[site_name]/assets/css/style.css


It seems the server gives you different paths based on the header.  You can change the hosts file to include the server IP and web address and view the website using it's full domain name, and that's fine (and I assume changing name servers to the hosting company's would work too), but using the IP to get to the site doesn't work.


The client says this problem is fixable using something called "rewrite root", which lets you update the mod rewrite path in the .htaccess file.  I've not been able to find out what he's talking about, and am not sure how it would work either.
(Would it force the server to return the correct path?)


Is it possible to do this?
Or is there another way?

I'll include the current code in .htaccess if it's relevant.


Thanks in advance.
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Open in new window

Avatar of caterham_www
caterham_www
Flag of Germany image

If you reach the .htaccess file in /some/physical/path/[site_name]/public_html/.htaccess via /[site_name]/.htaccess, set


RewriteBase /[site_name]/

Open in new window

Avatar of lucidica

ASKER

Do I set it here?
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /[site_name]/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Open in new window

The above doesn't work, unfortunately.

Still get the wrong paths.

Do I need to modify the mod rewrite rules to take off the public_html?
(And then only for the IP, as it will break if we then use with the domain name)
That would be fine; the order does not matter for that directive.
> The above doesn't work, unfortunately.

That means there's anther problem.

> Still get the wrong paths.

Wrong paths? The RewriteRule maps resources which don't exist as a file or directory to index.php, nothing more. Are you talking about HTML source code?
The CMS that's being used creates URLs based on the location of the scripts.  Mod rewrite then modifies these.

Example of the path on the server is:
...sites/[site_name]/public_html/assets/css/style.css

The URL after mod rewrite when site works (using domain name):
http:/assets/css/style.css

The URL after mod rewrite when site does not work (using IP/name):
http://[site_IP]/[site_name]/public_html/assets/css/style.css


Does this help at all?
> The CMS that's being used creates URLs based on the location of the scripts.

That is not an issue of mod_rewrite, if the CMS creates wrong paths, the client will receive the source code with the reference of resources created by the CMS.

mod_rewrite can't modify the HTTP response/source code. What you can do is to correct a "wrong" request by a client of /[site_name]/public_html/assets/css/style.css and redirect the request to /[site_name]/assets/css/style.css

The .htaccess file is in /physical/path/to/[site_name]/public_html/.htaccess, correct?

# redirect requests
RewriteRule ^public_html/(.*) /$1 [R,L]

Open in new window

Sorry, I do no know what I'm talking about!

Mod rewrite does nothing to the URLs, just maps them elsewhere?

<--- not familiar with this

So if the URLs are only mapped with mod rewrite, you'd have to modify it to map:
http://[site_IP]/[site_name]/public_html/assets/css/style.css
to
http:/assets/css/style.css
I think?
Posted too late - thank you!

Yes that path to .htaccess is correct - I'll give that a go and get back to you in a minute.
it should be

/sitename/$1

of course
Sorry, whereabouts do I put that line?
I've tried the following but it's not working (because I've done something wrong, I suspect).
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /brett-lesage.com/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA]
    RewriteRule ^public_html/(.*) /$1 [R,L]
</IfModule>

Open in new window

above of
    RewriteCond %{REQUEST_FILENAME} !-d

otherwise the rule will catch that request and rewrite to index.php.
RewriteEngine On
RewriteBase /brett-lesage.com/
RewriteRule ^public_html/(.*) /sitename/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA]

Open in new window

Ah it works, thank you!

There is one more thing though - is it possible to have this rule apply only when IP/site_name is being used?

(It breaks the site when the domain name is used)
> (It breaks the site when the domain name is used)

I think the rewritebase directive will brake your second rule, if used with the domain. You can remove the rewritebase directive if your second rule works fine without it for an IP/sitename request.
Taking it out worked when using the domain, but broke when using the IP/site_name, unfortunately!
ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Pretty sure that worked, but it's giving me another problem now:

"You don't have permission to access /index.php on this server"

When accessing using the IP/sitename.

Permission on index.php is 644.
In fact, it's not working.

I didn't change the "sitename" on the last line before, but when I do it gives me a "[sitename] not found" error.

I've tried taking that folder out altogether too but that doesn't work either.
No, that didn't work since it looks like the second rule matched because you shouldn't get a message for /index.php otherwise. Can you check, e.g., with php the value of $_SERVER['HTTP_HOST']? The IP should correspond to the one you're using in both conditions.
> I didn't change the "sitename" on the last line before, but when I do it gives me a "[sitename] not found" error.

You need to adjust "sitename" to the path and the IP to the actual one of course.
Sorry I stil can't get it to find the site name

I've tried putting the base in as well but it's not working either.

Does the 13th line of code need to be [site_IP]/[site_name]?
RewriteEngine On
RewriteRule ^public_html/(.*) /[site_name]/$1 [R,L]
 
RewriteCond %{HTTP_HOST} !=[site_IP]/[site_name]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
 
RewriteCond %{HTTP_HOST} =[site_IP]/[site_name]
RewriteBase /[site_name]/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

Open in new window

RewriteCond %{HTTP_HOST} !=[site_IP]/[site_name]

only [site_IP], nothing more in line 4 and 9.

> Does the 13th line of code need to be [site_IP]/[site_name]?

Only /[site_name]/index.php....

and no RewriteBase (the substitutions are absolute paths now).
RewriteEngine On
RewriteRule ^public_html/(.*) /[site_name]/$1 [R,L]
 
RewriteCond %{HTTP_HOST} !=[site_IP]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
 
RewriteCond %{HTTP_HOST} =[site_IP]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /[site_name]/index.php?url=$1 [QSA,L]

Open in new window

Sorry it's taking me a long time to reply - I'm getting quite confused!

That code is still giving me the same error:

The requested URL /[site_name] was not found on this server.

I think perhaps the server is responding really slowly - I changed back to the old code (the one that worked for the IP) and that gave the same error, after a while it started working.

I looked in the logs and it gave me errors about reaching the maximum number of internal redirects (10).

I  used the old code, and tried to put RewriteOptions MaxRedirects in, but it gave me server configuration errors immediately.  However, after taking them out and putting exactly the same thing back in again a while later it's just giving me the same 404 error.

So I'm not really sure if it's working or not, or what the server is up to...


(Here is the old code with the MaxRedirects in anyway)
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteOptions MaxRedirects=20
    RewriteBase /[site_name]/
    RewriteRule ^public_html/(.*) /[site_name]/$1 [R,L]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA]
</IfModule>

Open in new window

I'm not really sure what flagged up the errors by the way - it's only given the errors in two different time periods and I can't remember what I was doing then...(I will have to check it more frequently)
Tried it again and it seems to be working!

Perhaps it was because I had too many instances of the website open in different browser windows?  It's worked now I've closed all but one.

I'm going to try again tomorrow morning just to make sure. :)
Working, thank you a huuuuuuuge amount for your help!
You are excellent, thank you so much. :)