RewriteCond seams not to work well - .htaccess

I have a htaccess with this content:
RewriteEngine on
RewriteBase /new/
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(.*)\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
The URL im working on is some like:
http://www.example.xy/new/
When I call a link like this:
test
URL should come up with
http://www.example.xy/new/aaa/bbb/ccc
Problem is, no matter what I do, all my css, js, and images are gone because I have them included like
<link href="style.css" ... />
this ends up in
<link href="aaa/bbb/ccc/style.css" ... />
how can I deny this or what am I doing wrong?
My .htaccess is based on several tutorials and some qoogle searched posts...

RewriteCond seams not to work well
works fine, It's the implementation
Add this to your html head section to fix your css, js problem.
<base href="http://www.yoursite.com" />

Related

Rewrite rule so that css and js files with absolute paths are loaded correctly

It took me 2 hours but I can't figure it out and I don't know how to google a solution.
This is my Rewrite-Rule in the .htaccess-file:
RewriteCond %{REQUEST_URI} ^/blog/(.*)
RewriteRule ^blog/(.*) http://localhost:2368/$1 [P]
When I call example.com/blog, it redirects my to my ghost-blog running in nodejs (port 2368).
The problem: The blog itself loads css and js files with absolute paths:
<link rel="stylesheet" type="text/css" href="/assets/built/screen.css" />.
So it tries to load example.com/assets/built/screen.css.
But the file is reachable at example.com/blog/assets/built/screen.css
How can I solve this?
You have 3 alternatives:
Change the links/scripts source to /blog/assets/...
Change the file locations to /assets
Use a RewriteRule
.htaccess
RewriteCond %{REQUEST_URI} ^/assets(.*)\.(css|js)$
RewriteRule ^(.*) /blog/$1 [L]
RewriteCond %{REQUEST_URI} ^/blog/(.*)
RewriteRule ^blog/(.*) http://localhost:2368/$1 [P]
Here's the rule working: .htaccess tester
I assume that the files are being served by apache2 and not by the ghost-blog running in node.js. But if it's the latter,it will still work since after redirecting it will go the next rule. But you can also do the following:
RewriteCond %{REQUEST_URI} ^/assets(.*)\.(css|js)$
RewriteRule ^(.*) http://localhost:2368/$1 [L]

RewriteRule not working for multi URL

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9-+/]{3,}\ /+company\.php\?title=([^\s&]+)
RewriteRule ^ %1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9-+/]{3,}\ /+project\.php\?compname=([^\s&]+)&prjt=([^\s&]+)
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([^/]+)/?$ company.php [L]
RewriteRule ^([^/]+)/([^/]+)/?$ project.php [L]
what I expect is http://website.com/compname=Company&prjt=Project should look like http://website.com/Company/Project. And the same way company.php page also. Company.php page s working as expected, But project.php page is not working. Url is showing as expected, But page opening with complete errors. Any help is highly thankful?
If you're missing styles and scripts, your problem is most likely that you're using relative links in your content. When you change the URL from
/project.php
to
/company/project
any relative URL's will have the base change from / to /company/, which is obviously not what you want.
Either make all your links absolute URLs that start with a / or add a base to your page headers:
<base href="/" />
Company page is coming here without style sheet (messy look).
That's what I was asking you. We need to know the specific problem because opening with errors is not the same as your style sheet is missing.
Fix your stylesheet problem by adding this tag in your <head> section.
<base href="http://www.website.co" />
This happens when using this type of mod_rewrite and relative URL's in your html for CSS/JS etc.

Htaccess Rerwite Urls

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^hash/([a-zA-Z0-9_-]+) hash.php?key=$1 [QSA,L]
so that transforming url from
hash.php?key=anything
To
hash/anything
Works fine
But when i goes to hash/anything all urls in page have this schema
hash/anything/some_image.jpg
but i want to refer all links to root because that is meanningless
hash/some_image.jpg
so how can i refer all links to root when iam navigating to diffrent hashes !!
Just add this in your html page header:
<base href="/">

CodeIgniter Assets Not Found

Please help with CodeIgniter assets. I tried most of the solutions from StackOverflow (including the autoload utility helper), nothing works so far. Here's my setup below:
Folder Path:
.htaccess
<IfModule mod_rewrite.cs>
RewriteBase /hnk_ci
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
When I view source code, it looks like this:
<link href="http://localhost/hnk_ci/assets/css/bootstrap.min.css" rel="stylesheet">
But when click on the css link from source code, I get this error:
The requested URL /hnk_ci/assets/css/bootstrap.min.css was not found on this server.

CodeIgniter relative links without base_url()

My site is on "localhost/mysite" but when I view it on the browser, layout and links are broken. CSS and JS files are on "localhost/mysite/themes/" but this is what I see in the source code:
<link href="/themes/new/css/style.css" rel="stylesheet"/>
<script src="/themes/new/js/scroll.js"></script>
And when I click on the links above, I always get to the following addresses and they are broken:
http://localhost/themes/new/css/style.css
http://localhost/themes/new/js/scroll.js
"mysite" path is already missing. It should be:
http://localhost/mysite/themes/new/css/style.css
http://localhost/mysite/themes/new/js/scroll.js
On htdocs\mysite\application\application\config\config.php, I have this setting and it should be left unedited.
$config['base_url'] = '';
$config['index_page'] = '';
On htdocs\mysite.htaccess, I have this setting, and it's probably the file that needs changes.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I looked around and tried all the suggestions by changing .htaccess but nothing really helped to make it work. It's funny when I change
href="/themes/new/css/style.css
to
href="".base_url()."themes/new/css/style.css"
everything works perfectly. But this is not the solution. It's a big site and I shouldn't be editing too much files by adding base_url() function, etc. Maybe we can do something on .htaccess? :(

Resources