Problems with CSS while using RewriteRule on .htaccess - html

I'm having some trouble while using RewriteRule on my local and remote servers.
Here's the situation I'm in right now:
localhost/puntvallesgit/index.php?seccion=nosaltres
this is the actual URL
I want it to be renamed to:
localhost/puntvallesgit/nosaltres
and
localhost/puntvallesgit/nosaltres/
Both urls work, meaning, it does not give me a 404 or something like that, but, with the second one (localhost/puntvallesgit/nosaltres/) there's no CSS nor JS loading correctly.
Looking the html code, I can see that while using the rule, the url path for JS and CSS gets changed to:
localhost/puntvallesgit/nosaltres/css/styles.css
yet the code itself has this:
<link href="./css/styles.css" rel="stylesheet">
I understand that sine I'm rewritting the url, to /nosaltres ... the html code adapts to that new folder structure, but that is not the expected behaviour.
Also, while using the url without the final slash (localhost/puntvallesgit/nosaltres) it works great (under localhost).
I could use absolute paths, yet, that is not the most ideal thing to do, since then paths would change from my localhost to my remote server
Here's the htaccess rewrite i'm using:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?seccion=$1 [NC]
RewriteRule ^([^/]*)/$ index.php?seccion=$1 [NC]
</IfModule>
Some advice please?
Thanks in advance

You can add the base tag between the <head> </head> section of your site. Then your asset folders will read from the root.
<base href="/">
Also rewritecond only works for the first rewrite following it. You can condense your rule this way.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]*)/?$ index.php?seccion=$1 [NC,L]
</IfModule>

This is not htaccess related (although your rewrite rules are not optimal), but rather a problem in the way you load your stylesheet.
When browsing to http://localhost/puntvallesgit/nosaltres/, the browser will try to load your stylesheet from ./css/styles.css which translates to http://localhost/puntvallesgit/nosaltres/css/styles.css, while your stylesheet is probably at http://localhost/puntvallesgit/css/styles.css.
You should use /puntvallesgit/css/styles.css as the source of your stylesheet instead.

Related

Force Trailing Slash + Remove .html and website keeps loading error

I am having problem with adding trailing slash end of URL. I have tried many suggestions from overall web but came up with no successful outcome. Hiding .html works fine, however adding slash does not really work. Main issue is that after I apply code below, the slash appears but website or any page gets never loaded. It keeps in loading mode. I would appreciate if someone helps me through.
My code in .htaccess is:
ErrorDocument 404 /thanks.html
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.html(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html$ http://%{HTTP_HOST}/$1 [R=301,L]

How to make .html appear as / in .htaccess

My problem is this,
I want for example:
Make www.mywebsite.com/index.html
Look/appear (not redirect) like this
www.mywebsite.com/index/
so far I used below code and only obtained www.mywebsite.com/index look.
I will highlight that I don’t want to redirect, I want only to the html look like folder
I work in Adobe MUSE. I build websites mostly for fun and just want to focus on designing not coding
Here is my .htaccess script taken from here: .htaccess rewrite /foo/bar/ to /foo/bar.php
Please, can someone help me with it?
What line(s) should I change to have the folder appearance
Thanks
Options -MultiViews
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Za-z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Making a resource (like a pdf) available only if its URL is called from a certain page

I have a pdf file (myFile.pdf) and an HTML page (myPage.html) which contains the following markup:
<a href='myFile.pdf'>Click here to view PDF</a>
Is there a way to make so that the pdf is ONLY reachable when clicking on the link contained in myPage.html and in all the other cases the user gets a redirect to myPage.html ? It's different from hotlinking because it should also prevent direct accessing the URL by typing the address of the PDF. The most similar problem I found so far is here but it does not fully apply to this particular case.
Cheers!
Try this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://www\.domain\.com/.*
RewriteCond %{REQUEST_URI} ^/myPage.html
RewriteRule .*\.pdf - [L]
RewriteCond %{HTTP_REFERER} ^http://www\.domain\.com/.*
RewriteCond %{REQUEST_URI} !^/myPage.html
RewriteRule .*\.pdf /your_page [R,L]

Access CSS file after htaccess modified url

I am using this htaccess file to re-write my urls:
RewriteEngine On
# Friend SEO url
#RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^\ ]+)\.php
#RewriteRule ^ /%2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
The url rewriting works perfectly fine (for example, mypage/web-store/ can be used instead of mypage/web-store.html).
The problem however, is that my rewritten url can't retrieve the CSS file correctly. If I use the normal url however, I have no problem at all because the CSS file is in a subfolder of mypage. But with the url rewrite, it looks for the css file inside the folder /web-store/, which doesn't exist...
Anyone knows a way to counter this ?
You need to either change your URLs to absolute URLs (add a leading /) or you can set a relative URL base in the header of your pages:
<base href="http://example.com/mypage/" />
Where "example.com" is the domain name of your site and the /mypage/ is where the css is at.
If all else still fails, you can try to rewrite the proper page:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+\.css)$ /path/to/styles/$1 [L,R=301]

Loss of format due to .htaccess

I'm making a .htaccess for my web page, here's the code:
Options -Indexes
RewriteEngine On
RewriteRule ^([A-Za-z]+)/([0-9]+)/$ files.php?row=$1&column=$2
RewriteRule ^([A-Za-z]+)/?$ $1/1/
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mywebpage\.at [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} ^http://.*$
RewriteRule \.(jpe?g|gif|bmp|png)$ /imgs/hotlinks.png [L]
RewriteCond %(REQUEST_URI) ^imgs/$ [NC]
RewriteRule / - [F]
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
The problem is, my images are not showing up in pages with rewritten URLs. Apparently, their routes are also affected by the rewriting. Simply explained: an image located at www.mywebpage.at/imgs/pic1.jpg loads on www.mywebpage.at/files.php?row=first&column=3, but when I try to enter www.mywebpage.at/first/3/ the browser looks for the image in www.mywebpage.at/first/3/imgs/pic1.jpg, and obviously fails to load it.
How can I fix this so that the routes of the images do not change, no matter from where I use them?
You can do:
RewriteCond $1 !^(imgs)
RewriteRule ^([A-Za-z]+)/([0-9]+)/$ files.php?row=$1&column=$2
That will rewrite everything except the imgs folder.
Edit: Apologies, misread question.
Make your image paths absolute. E.g. relative path is:
imgs/image1.jpg
Absolute path is:
/imgs/image1.jpg
The slash at the beginning tells it to go to the very root of the site and then go to imgs, then image1.jpg.