Access CSS file after htaccess modified url - html

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]

Related

How to add trailing "/" to website with .htaccess

Hi i'm trying to add a trailing "/" to my website for the past few weeks and every code and website tutorial to edit the .htaccess i've tried has failed to work. All my webpages end with ".html". The first part of my htaccess code forces the use of "https" and "www". What I want is that + when a user clicks on a link on my site to say "a href="help.html" it will load help.html but it will appear in the address bar as "https://www.example.com/help/" I don't care if I need to change all my links to omit the ".html" or if I need to change all my filenames to omit the ".html" extension. I just want that trailing "/" + my www and https force. Here is my code so far:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=302]
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=302,L]
P.S. I am aware of the 302 redirect as i'm testing, once my code is working i'll change to 301. Any help would be appreciated.
Add this and If the URL doesnt have a trailing slash the htaccess will redirect the browser to the URL with a the slash.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

.htaccess: remove .html and redirect slash version

On my site, all pages have .html extension and are directly off the root (i.e., site.com/page.html). I've successfully used the following .htaccess code to make .html not show:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
This gives me a url like site.com/page, but in the event that a user links to or visits the url using a slash (i.e., site.com/page/), I'd like to 301 redirect to the non-slash version.
I'm having trouble integrating this part into the above code. Thanks in advance for any suggestions.
Try to something like this
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.example.com/$1/ [L,R=301]

subfolder and html page name is the same and nor working

I have a webpage, which has got a html page called "blog.html" and a subfolder which is called "blog" and have some other html page inside. I just removed .html with htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Now, when somebody click on the "blog" menu, the following happened:
You don't have permission to access /blog/.html on this server.
I wonder how can i fix this, i would like to have a page with working urls like this:
mydomain.com
mydomain.com/blog
mydomain.com/blog/first_post
Change your rule to this:
DirectorySlash Off
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]
And test it in a different browser.

redirect subfolder to root with <subfolder-name>.html using htaccess

I have website that has links like
domain-name.com/pages/contact-us/
www.domain-name.com/pages/contact-us/
domain-name.com/pages/about-us/
www.domain-name.com/pages/about-us/
and other pages ...
What I'm looking for how i can redirect them permanently to same subfoldername.html using htaccess . for example for all the links above should redirected to
domain-name.com/contact-us.html
www.domain-name.com/contact-us.html
domain-name.com/about-us.html
www.domain-name.com/about-us.html
also for other pages, otherwise if page not found to root index.html .
Thanks in advance.
Try the following
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(/pages/(.*[^/])/?)$
RewriteRule ^(.*)$ /%2.html [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.html [L,R=302]
The first three lines will remove the folder pages. If there is a trailing slash, it will be removed. The last three lines will redirect to /index.html, if the request file does not exist.

.htaccess removal of extensions

I'm having some troubles with my .htaccess file, I have code placed in which removes the extensions of files so it looks like this: www.foo.com/x and it works fine for all of my pages. However, when I use my navigation bar it doesn't work. I've already changed the URLs on each page to www.foo.com/x, so I'm confused as to what my problem could be. Here's the code in my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And here's my nav-bar code:
<ul id="menu">
<li>Home</li>
<li>Projects</li>
<li>About</li>
<li>Contact</li>
</ul>
I might add that www.foo.com/projects, etc. works fine and goes to the correct page also my .htaccess file is in the root folder if that wasn't obvious.
Your original rules are going to cause a loop if you try to internally rewrite them back. For example, if you start off with the URI /some/file.html
Browser requests /some/file.html
RewriteRule ^(.*)\.html$ /$1 [L,R=301] matches, browser is redirected to /some/file
Browser requests /some/file
Internal rewrite to /some/file.html
Rewrite engine loops, first rule matches again, browser is redirected to /some/file
repeat from #3
So you need to match against the actual request for your first rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
RewriteRule ^ /%1 [L,R=301]
This does the redirect, in case you have links that still go to the .html files, now you need to internally rewrite them back to html files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ /$1.html [L]
If all of your links already have the .html part removed, then you won't need the first rule at all.