I have a website and I decided to add a sub-domain:
http://subdomain.dommain.com
However, when I created subdomain I chose Document root as: public_html/subdomain to make it easier to include files located there, such as header and footer, for example. Indeed, using a relative path I was able to include them with ease, but my header had some references to CSS and JS files that do not load when I access my sub-domain.
On top of it, I have some overwrite rules in .htaccess file that simplify some inclusions. For example:
RewriteRule ^css/(.*)$ /include/assets/account/css/$1 [NC,L]
How can I fix it?
You can try using HTML BASE tag
For ex is your images base path is http://domain.com/css/ then put this snippet in HEAD section of your page:
<base href="http://domain.com/css/">
Related
I would like when accessing the url https://www.italinea.com.br/uploads/jx5rufam7adfwd75pi6c.jpg, which is an existing file on the server, open the php file https://www.italinea.com.br/image.php.
Which htaccess rule do I use?
I tried to use:
RewriteRule ^(.+)\.jpg image.php [L,QSA]
But as it is an existing file, it opens the image and not the .php file
RewriteRule ^(.+)\.jpg image.php [L,QSA]
This is the right idea and should "work", although it could be simplified a bit. And it is rather generic, so matches every .jpg request.
If this is not working then either:
You have a conflict with other directives in the .htaccess file (the order of directives can be important).
You have a front-end proxy that is serving your static content. This is a problem if the URL being requested maps to a physical file as the application server (ie. .htaccess) is then completely bypassed.
If this is the case then see my answer to the following related question:
WordPress: can't achieve direct image access redirection via .htaccess
I wanted to add a subdirectory to my url so it would become easier to read:
Example of what i'd like:
localhost/testwebsite/users.php?firstname=john
should become
localhost/testwebsite/users/john
I tried using the .htaccess mod_rewrite rules and came up with this:
RewriteEngine On
RewriteBase /testwebsite/
RewriteRule ^users/(.*)$ users.php?firstname=$1
What happens why I use that code: it redirects the page successfully, it shows the html of the correct user and it processes the argument correctly. However, all stylesheets, images, scripts, anything with a relative path, could not be found and respond with a 404 message, because of the extra subdirectory added in the new url, I reckon.
Am I doing something wrong? Is there another technique I should be using? Or should I simply make all paths in my project absolute with regards to the root?
You're doing it right. The browser doesn't know that the actual path of the file is different. Use absolute paths or make paths relative to the easier to read URL.
Following up on a previous question I asked (htaccess load page B instead of page A without redirecting) I've run into this issue:
user goes to .com/A , then .com/B is loaded without changing the URL. this is fine.
But, the Rewrite rule also allows for a trailing slash and anything that would follow:
RewriteRule ^(a|b|c)[\/.*] /d [L]
The problem is, when .com/A/ is loaded, all relative hrefs and links point to that 'fake' /A/ folder.
What should I do to prevent this, and have .com/A/ (with the trailing slash) still act as if it is part of the root directory that .com/index.php would use?
Thanks!
One thing to note with rewrites. I would use absolute paths or / in front of the path, for CSS and JS files. relative paths will mess you up every time.
I have a page deals.php which is placed at root directory that is being accessed by number of internal URLs with multi directory levels. for example this page would be accessed by both following urls.
http://domain/flights/asia/bangkok
http://domain/flights/bangkok
I am using this code in .htaccess to redirect to deals.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^flights/asia/([^/]+)/?$ deals.php?country=$1 [NC]
RewriteRule ^flights/([^/]+)/?$ deals.php?country=$1 [NC]
Now the problem here coming is that on deals.php all the images, script and style sheet files are not opening properly. If I try to fix it against one URL by placing ../../ in addresses of all images, script and css, it dont work for other URL.
How to solve this problem? what are the options?
Easy: DO NOT use ../ in links to any resources (images/css/js/etc) -- always use URL that is relative to the WEBSITE ROOT -- that is a "requirement" when you dealing with nice/not-real/rewritten URLs as such URL rarely points to the physical file location.
Lets assume you have a logo that is located at http://www.example.com/images/logo.png.
Now, instead of ../images/logo.png and/or ../../images/logo.png ALWAYS use /images/logo.png.
i think what you want is placing an baseurl in html page, so all the relative path will related to the baseurl, not the current URL in the navigator bar
I have an htaccess file which maps http://www.myserver.com/home/ to http://www.myserver.com/index.php?section=home
This part works fine. The issue I am facing now is, all my images and css reside in a sub-folder named assets, i.e. http://www.myserver.com/assets/images/ http://www.myserver.com/assets/css/ etc.
After redirection the browser will look for the files under http://www.myserver.com/home/assets/images/
which causes things to break, as this is not a valid path.
I have been able to add another rewrite that maps the above to the correct sub-folder, however, Firebug shows that the images are residing in: http://www.myserver.com/home/assets/images/
I know it's not really a problem, after all, my images and css are loading just fine with this rule.
I'm just curious as to how I could make the path shown to be the actual path, which is: http://www.myserver.com/assets/images/
Pasting my htaccess file below. Thank you very much beforehand.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/]+)/assets/(css|images|js)/(.*)$ /assets/$2/$3 [NC,L]
RewriteRule ^([^/]+)/$ /index.php?section=$1 [NC,L]
The problem is that you didn’t consider that relative URLs are resolved on the base URI that is the URI of the HTML document the reference is used in. So a relative URI path like assets/images/ in an HTML document with the URI path /home/ is resolved to /home/assets/images/ instead of /assets/images/.
You cannot change this with mod_rewrite as URI resolution is done by the client and not by the server. The only solutions are:
change the base URI using the BASE element (note that this affects all relative URI);
using absolute URI paths, e.g. /assets/images/ instead of a relative assets/images/;
adjusting the relative URI path, so references in /home/ are adjusted to ../assets/images/ to reflect the path depth.
Add this line <BASE href="http://www.yoursitename.com/"> to your page inside the <head> tag as the following:
<head>
<title>Your site title</title>
<BASE href="http://www.yoursitename.com/">
....
</head>