Make domain folder act as root - html

I have a domain for example https://test.com this domain contain a full project in https://test.com/system.
Problem:
Whenever I write <link ref="stylesheets" href="/style/main.css"> it search in the domain root not in the system directory which the project in.
What I have tried to do:
I have tried to edit all my path to be equal to href="style/main.css" instead of href="/style/main.css".
This solution works but I won't do this every time I go in production mode.
What I am trying to do:
I know there is a solution using .htaccess but I don't know how to write it..

Try
<link ref="stylesheets" href="./system/style/main.css">
or
<link ref="stylesheets" href="../system/style/main.css">.
If it still won't work, use three dots.
<link ref="stylesheets" href=".../system/style/main.css">.
It has worked for me.
To set a subdirectory as root with .htaccess
It will depend, with some hosting services you only need to add this line:
RewriteBase /system
If that doesn't works for you, try this:
RewriteEngine On
# Map http://www.example.com to /system.
RewriteRule ^$ /system/ [L]
# Map http://www.example.com/x to /system/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/system/
RewriteRule ^(.*)$ /system/$1
# Add trailing slash to directories within system
# This does not expose the internal URL.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^system/(.*[^/])$ http://www.example.com/$1/ [R=301]

Related

Trailing Slash Issue with URL for Subdirectory

I am currently using one host to host multiple domains (each domain is pointed to a folder in the root directory). Which generally works fine, but the problem comes when trying to access sub-directories of a domain.
So for example, I have the domain example.com which is pointed this way
example.com points to \folder\abc\
https://example.com works fine
https://example.com/xyz/ works fine, where xyz is a sub-directory in the abc folder.
https://example.com/xyz redirects to https://example.com/folder/abc/xyz/
Is there something I need to do in my .htaccess file or web.config to fix this issue where URLs without trailing slash point in a different way?
--
Update:
This is what my .htaccess rules look like:
RewriteCond %{HTTP_HOST} example\.com$
RewriteOptions AllowNoSlash
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} (.*)
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.+[^/])$ %1/ [R=301,L]

HTML always loaded from cache

I have a index file under www/ and apache config set DirectoryIndex to that index.html
If I use this link
example.com/
Everything works fine since the html itself has meta tag to not use cache
but if I use
example.com (Which in url bar will redirect to example.com/)
//Note: In chrome, the initiator for this also change to example.com
The index is always fetched from cache
And the second way is how normal end user type in the address. Can you guys explain what is going on?
example.com/ is treated differently than example.com. The slash indicates that a URL is a folder and not a document. Adding extra slashes (such as example.com///) will also consititute an independent caching in this regard.
I would recommend forcing a single trailing slash, which can be done with the following .htaccess:
RewriteEngine On
# Assuming you're running at domain root. Change to working directory if needed.
RewriteBase /
# www check
# If you're running in a subdirectory, then you'll need to add that in
# to the redirected url (http://www.example.com/subdirectory/$1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Finally, forward everything to your front-controller (index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [QSA,L]
More information regarding this can be found here.
Hope this helps! :)

Using .htaccess to RewriteRule but every file points to one file

I am currently trying to using .htaccess to do the following:
When b/ is found in the url, it redirects to browse.html. My .htaccess file looks like:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^b/(.*)$ browse.html [NC,L]
I am using Apache version 2.2.31 and here is what my directory looks like:
Root
Root/browse.html
Root/[css]
Root/css/browse.css
Root/[js]
Root/js/browse.js
When I do this, it brings me to browse.html but nothing loads correctly. Every file in the css and js directories become browse.html. Is there something extra I need to add to this?
Here is what the dev tools directory looks like with the .htacess
localhost
b/?page=1 (browse.html file. Name comes from query string)
b/[css]
b/css/browse.css (blank file)
b/[js]
b/js/browse.js (browse.html instead of browse.js)
I have tried putting RewriteBase / in between but that does nothing to the problem.
Your rewrite rule is fine. Problem is happening due to your use of relatives paths for css/js/images. While resolving relative paths browser appends it to current page's path.
You can add this rule above your existing rule::
RewriteEngine On
# fix css/js paths
RewriteRule ^b/.*((?:js|css)/.+)$ /$1 [NC,R=301,L]
RewriteRule ^b/(.*)$ browse.html [NC,L]
Make sure to ignore requests if files/folders exists:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^b/(.*)$ browse.html [NC,L]

Moving From Development to Live Site breaks CSS

Using CodeIgnitor to manange this site I have configured the css in the following location:
{link rel="stylesheet" type="text/css" href="<? echo base_url();?>application/assets/css/public.css" />
Firebug shows me that the link points exactly where it should, unfortunately,
The problem is I keep getting 403 errors when it tries to load it.
my .htaccess file looks like this, and by the way, I don't really know what I'm looking at here:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|application/assets/js|application/assets/css|application/assets/images)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond $1 ^(images|js|css)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./public/$1 [L,QSA]
How do I get this CSS to load?
Additional Info:
In that the assets folder there is also a js folder and an images folder and the favicon is NOT being pulled from that images folder either.
It sounds like the configuration on that folder and/or the server are refusing you access to read the file.
http://en.wikipedia.org/wiki/HTTP_403
Try navigating directly to the css file and you can easily confirm that you are getting a 403 error, if so it is to do with permissions.
Try accessing your development site remotely rather than from the same machine and you may see the same error there.

.htaccess rewriterule /state/city/

This will take a bit of explanation so I hope I don't lose everyone here.
I needed to get something like the following:
http://example.com/results.html?state=iowa&city=davenport
turned into:
http://example.com/iowa/davenport/
I was able to accomplish this with the use of these two rewriterules:
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1 RewriteRule
^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
The problem is that in the backend there is "some code somewhere" that is getting broken as a result of the second rewriterule. It has to do with filling in a select box based on the results of another one selected (I don't think that matters though). I think the problem is in that I'm modifying too broadly the /state/city.
Here is a copy of my full (modified for security) .htaccess file:
IndexIgnore *
AddHandler application/x-httpd-php5 .html .htm
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
<Files .htaccess>
order allow,deny
deny from all
</Files>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/USER
<Files php.ini>
order allow,deny
deny from all
</Files>
The code that its screwing up is very complex and its someone else's code. After a couple of hours I've been unable to wade through all of their stuff to even come close to what I may be able to change on their end to get things working.
Does anyone have ANY ideas on what I could do to avoid this problem? I really only have 3 .html files that I'm funneling my frontend code through so I had tried something like a
my rewriterules
and same with using just "files" instead of filesMatch. Everything I've come up with breaks something else or the entire site in one way or another.
First: (i) hostgator won't enable or give you access to rewrite logs; (ii) your suPHP config has syntax errors and hostgator almost certainly does spme of this and the .htacess / php.ini denials in its own root / vhost configs. However, I'll focus on the mod_rewrite elements:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
I am also assuming that you don't have any .htaccess files in subdirectories with the rewrite engine enabled as these could preempt this under rewrite "Per Directory" precedence rules.
Rules (3) is a simple domain redirector. Rule (4) is a draconian: redirect any URI which is not an existing file or directory to index.php in the current directory, but leaving the query string intact.
Rule (1) and (2) are your new rules. As Mike says, you should include the [L] but since the files cities.html and results.html exist it won't match anyway.
I am curious as to why the trailing slach in the URIs is optional. Better to decide and to fix this.
The issue is that the match criteria for (1) and (2) are two broad and are picking up URIs intended for the general catchall (4). You need to lock this down to make these mutually exclusive. One why is to mine your access logs (which are available with hostgator) to find the standard URIs which the application expects and check that none match (1) or (2) -- However, since most will include a ".", this probably isn't the case. But check.
The other issue is whether the existing scripts use absolute or relative references e.g. <img src="images/myimage.png"> in any output HTML. Here the browser has asked for http://www.example.com/texas/houston say and will therefore look for http://www.example.com/texas/images/myimage.png which doesn't match (1), (2) or (3) and therefore is caught by (4) and passed to /index.php. Ditto CSS files etc. Hence they won't 404 and index.php will get confused and send some default response which will hopelessly confuse the browser.
However, again analysis of the access logs (in this case or USIs with a referrer http://www.example.com/texas/houston) will show you if this is going on.
If your app uses standard subdirectories then you might be able to fix this by a rule (3.1) which looks something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1/$2 -f
RewriteRule .*?(images|css|styles)/(.+) $1/$2 [L]
though the details will depend on the rest of your application.
I was able to solve it by changing my (relevant) .htaccess entries to the following:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/?$ cities.html?state=$1
RewriteCond %{REQUEST_URI} !^/signup/
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ results.html?state=$1&city=$2
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
The addition being:
RewriteCond %{REQUEST_URI} !^/signup/
HostGator was able to find that the issue was /signup somewhere in a log somewhere, never did find out which log they were able to look at but I assume it was something I didn't have access to.