htaccess forward slash goes folder deep - php

I have this simple redirect rules:
RewriteEngine On
RewriteRule ^tags/([^\.]+)/?$ home.php?search=$1 [L]
So if a url looks like
http://example.org/path/to/folder/tags/exampleTag
It is redirected to
http://example.org.com/path/to/folder/home.php?search=exampleTag
It works, it redirects to home.php, but the problem is things and images on home.php. Apparently, the path when loading the images turns to
http://example.org/path/to/folder/tags/imgs/img.jpg
Instead of:
http://example.org/path/to/folder/imgs/img.jpg
I.e, during redirection, the home.php loads things like it is in the tags folder and because they are not, the images do not get loaded.
How can I solve this?
On home.php I am using html to load images like this:
Both the .htaccess, home.php and the imgs folder are on the same folder

Add this to the header of your pages:
<base href="/path/to/folder/" />
so that all of your relative paths use the correct URI base.

Related

while removing php extention in url it's not loading css files [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

Failed to attach all styles, scripts etc, Router and .htaccess [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

Redirect to dynamic relative paths with .htaccess and php? [duplicate]

I want to make my URL as SEO Friendly URL. I tried editing .htaccess file by rewriting rule
RewriteRule ^swift-details/([0-9]+)/([0-9a-zA-Z_-]+)$ swift-details.php?id=$1 [NC,L]
RewriteRule ^swift-details/(css|js|img)/(.*)?$ /$1/$2 [L,QSA,R=301]
It's routing the correct URL but in that page CSS JS and images are not working.
Example URL:
http://www.example.com/swift-details/2/abblinbb
This is because your relative URIs have their base changed. Originally, the base is / when the page is /swift-details.php?id=foo, and the browser properly fills in relative links with the / base. But when the browser goes to a page like /swift/details/foo the base suddenly becomes /swift/ and it tries to append that in front of all relative URLs and thus none of them load.
You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head> tags):
<base href="/">
You dont need the second rewrite rule. Your CSS/JS paths are all 'relative' to your current location.
Your CSS exists here:
/css/normalize.css
Your page is looking here:
/swift-details/2/abblinbb/css/normalize.css
All you need is 'forward-slashes' before your CSS/JS paths.

Page resources fail to load when removing '/' slash from URL upon .htaccess redirect

I have a simple redirect rule as follows:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^contact-us\/?.*$ ./contact-us.php [NC,QSA,L]
This rule works fine for the following URL:
http://localhost/folder1/folder2/folder3/contact-us/
The problem is that if I remove the final slash, the page loads properly but the resources (css/js) are not loading
http://localhost/folder1/folder2/folder3/contact-us ----> This fails
It seems it is omitting a folder when trying to load the resources so rather than http://localhost/folder1/folder2/folder3/js/jquery.js, the URL for the JS resource is being set as http://localhost/folder1/folder2/js/jquery.js
Is there a concept missing here with the rules? How to make both URLs work?
This is happening due to your use of relative paths in js/css etc.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/folder1/folder2/folder3/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

Using mod-rewrite to pass route to index.php

I'm kind of new to mod_rewrite so I need some help from you guys.
My folder structure is:
assets/fonts/ Contains all fonts
assets/images/ Contains all images
assets/scripts/ Contains all JavaScript files
assets/styles/ Contains all CSS files
pages/ Contains parts of the page as PHP file or directory containing PHP files
header.php Header of site
footer.php Footer of site
index.php index of site, now always loads pages/home.php.
The pages directory contains all the body parts of the pages like home.php, about.php etc. It also contains directories like 'portfolio' which in turn contains more .php files.
This is how I'd like my urls to be re-written.
http://domain.com/about => http://domain.com/index.php?route=about
http://domain.com/about/kittens => http://domain.com/index.php?route=about/kittens
This should however exclude requests directly to the assests or pages folder.
The reason why the pages folder should be excludes on direct requests like http://domain.com/pages/about is because some parts of the site are loaded with AJAX.
Preferable it would exclude any directory in the root.
Could you guys help me out?
You could use an approach along these lines:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|pages|robots\.txt)
RewriteRule ^(.*)$ /index.php?route=$1 [L]
Save this as .htaccess in your root folder.

Categories