URL rewriting messes css/js up - .htaccess

what I'm trying to achieve is
domain.com/register/free
that would be
domain.com/register.php?type=free
When I go go domain.com/register the page loads fine, but if I put the next /free part it cant find the css or js images etc. Not too sure where I have gone wrong but this is my .htaccess file:
RewriteRule ^register$ register.php
RewriteRule ^register/$ register.php
RewriteRule ^register/([a-zA-Z0-9\-\_]+)$ register.php?type=$1

First, you can improve your rules
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^register/?$ register.php [L]
RewriteRule ^register/([a-zA-Z0-9_-]+)$ register.php?type=$1 [L]
Then, since you create a virtual folder because of your rule (/register/) and you're using relative paths (instead of absolute paths), your links are not good anymore.
Two options (both using absolute paths):
add a leading slash for all your css (and js, img, etc) links.
e.g: href="/css/style.css" instead of href="css/style.css"
add this tag just after <head> in all your concerned pages: <base href="/"> (this will avoid you to replace each link one by one)

Related

htaccess | not looking in the right location for js

I am trying to write a htaccess file that will allow for /example/1/profile to look for a JavaScript file within /example/. Currently on Internet Explorer 11 it is looking for /example/1/file.js whereas realistically it should be looking for /example/file.js.
This needs to be done inside of the .htaccess file as the setup that the website currently has.
I know there is a way in which you can redirect 404 to /example however this is resulting in a 200.
Is their a way I can say in the htaccess file that if it is .js .css to look in /example?
Thanks in advance
EDIT:
For a little but more information, my current htaccess is like this
DirectoryIndex index.php index.html
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /example/index.php [NC,L]
It is for php because the php echos a file_get_contents of the index.html which is an Angular project.
So I need this htaccess to be the following logic
If the file is a .js or .css then rewrite the location to /example else rewrite the location to example/index.php.
The reason this is happening is because I am doing a format which has the ID as a second parameter and for some reason this is interfering with the way that the URL is structured for the js, css.
I imagine this line is what is breaking it...
RewriteRule ^(.*) /example/index.php [NC,L]
Converting my comments to answer. This appears to be problem due to relative links.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/example/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

css, images and vendor folders do not load when using htaccess rewrite

Below is the htaccess scripti am using:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^gecko/([^/]*)$ gecko.php?gecko=$1 [L]
Which changes http://localhost/geckology/gecko.php?gecko=Zilly (which loads css etc fine) to http://localhost/geckology/gecko/zilly which doesn't load the css etc fine as it looking in directories like this http://localhost/geckology/gecko/css/theme.css when it should be http://localhost/geckology/css/theme.css
The site is huge, so if possible i would like a htaccess way of fixing this, however i will change everything to absolute urls if it's not possible
You can fix the relative URI base by simply adding this to the header of your pages:
<base href="/" />
or if you have to use htaccess, which is really inefficient and will assume all of your css/scripts/etc are all in one place, while at the same time making the rest of the world think the same thing is actually 2 different URLs:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gecko/(css|images|other)(/.*)$ /$1/$2 [L]
This blindly rewrites any URL that tries to access "css", "image" or "other" folders within the /gecko/ path.

htaccess and redirection to a holding page

I'd like to redirect all outside visitors to a holding page whilst allowing all internal users to see the whole site.
I have the following
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteCond %{REQUEST_URI} !/holding-page/index.php$
RewriteRule .* /holding-page/index.php [R=302,L]
Which does what I want but the holding page won't pull through any styling or images.
Does anyone know who I'd achieve this?
That is due to the use of relative links on your holding page.
Insert one more rule to fix that:
RewriteRule ^holding/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [L,NC,R=301]
If that doesn't fix the problem you can adapt these:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can add this in your page's HTML header: <base href="/" />
I managed to resolve by allowing access to the /holding-page/ directory with the following rule
RewriteCond %{REQUEST_URI} !/holding-page/

htacces and the nice urls

I have this link formats on my webpage:
https://mypage.com/index2.php?page=registration
https://mypage.com/index2.php?page=food&category=1
The first type is replaced as this:
https://mypage.com/registration (works well)
And I would like to format the second as:
https://mypage.com/food/1 (doesn't work, the page is loaded, but the images don't)
So I created the following htaccess file:
RewriteEngine on
RewriteRule ^$ index.php [L]
RewriteRule ^([^/.]+)?$ index2.php?page=$1 [L]
RewriteRule ^food/([^/.]+)?$ index2.php?page=food&category=$1 [L]
But doesn't work. :(
What is wrong with this? And where are the pictures?
Thank you for your answer.
(doesn't work, the page is loaded, but the images don't)
Probably because your links are relative (doesn't begin with a /) and when you have that extra slash after /food, it changes the URL base (tries to access images in a non-existent /food/ directory). Change all your links to absolute URLs or add a base in the page header:
<base href="/" />

Multisite with htaccess

I'm new to this (wonderful) website!
I did search for an answear to my problem but I could not find it.
(Q: bad english. A: I'm italian, sorry.)
My goal:
I would host in a domain 2 different websites, each of them using their proper htaccess rules.
The structure:
/index.html --> empty page.
/site1/index.html
/site1/styleA.css
/site1/logo.png
/site2/index.html
/site2/styleB.css
/site2/logo.png
The problem:
/site1/index.html gets the stylesheet file using absolute path:
<link rel="stylesheet" type="text/css" href="/style.css" />
this way it try to get the "style.css" file in the root directory (404).
Same problem with site2 and every other file that use absolute path.
More info:
Sites are stored in folders only to test them, they will be moved to different domains once ready so I would not edit every absolute path.
I'm not using Wordpress, Drupal or similar. They're all my creations.
The Question:
Is it possible to solve the 'absolute path' problems using only .htaccess file(s)?
If not, what would you suggest me to do?
I suppose you can check the referer:
RewriteEngine On
RewriteCond %{HTTP_REFERER} /site1/
RewriteCond %{REQUEST_URI} !^/site1/
RewriteRule ^(.*)$ /site1/$1 [L]
RewriteCond %{HTTP_REFERER} /site2/
RewriteCond %{REQUEST_URI} !^/site2/
RewriteRule ^(.*)$ /site2/$1 [L]
This doesn't redirect the browser, so there's a possibility that the browser will cache a "/style.css" and the two sites get mixed, so you may want to redirect the browser instead by changing the flags from [L] to [L,R].

Resources