how to rewrite url with htaccess and keep relative links working - apache

I am using the following .htaccess rules:
RewriteRule ^!([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^!([a-zA-Z0-9_-]+)/$ index.php?p=$1
RewriteRule ^!([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?p=$1&s=$2
RewriteRule ^!([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?p=$1&s=$2
In order to rewrite the url from this: /index.php?p=SOMETHING&s=SOMETHING
To this: /SOMETHING/SOMETHING
The problem is - I am using relative urls for pretty much everything - css styles, scripts, images, etc.
And the current htaccess rules break the urls, cause they are trying to access files from a realtive path.
So I would like to know if there is any way to be able to still use these rules and keep the relative links working at the same time.
Any help is much appreciated!

You need to include the URI base in the header of your pages. You can add something like this in your page headers (inbetween the <head> </head> tags:
<base href="/">
Either that, or you can change all your links to absolute.

Related

Problem with htaccess when making friendly urls with the trailing slash does not work [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.

htaccess - File directory is being wrongly redirected [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.

.htaccess Rewrite Images to Different Directory [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.

Why can't i have slashes in my rewrite rule?

I am trying to make an .htaccess file, to create SEO url.
It seems however that I can't have a slash in the mod rewrite, am I doing this wrong?
My code is as follows:
RewriteEngine on
RewriteRule ^(.+)/$ /$1 [L,R=301]
RewriteRule ^folder/folder-name index.php?show=folder&folder_id=7
The page is loaded correctly, but the style sheet/images/javascript aren't found!
It's as if, the page thinks that it should look for the style/images/js relative to "folder/folder-name".
If i change the last line to:
RewriteRule ^folder-folder-name index.php?show=folder&folder_id=7
Then everything is loaded correctly, so i'm pretty sure it's because of the slash.
Any help?
It is because you're using relative paths in your JS, CSS etc. You have 2 options to fix it:
Just 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 try adding this tag in your page's header:
<base href="/" />
One alternative: You can use paths relative to site root.
i.e. change:
<img src="images/some.jpg" />
by
<img src="/images/some.jpg" />
Being images folder here: yoursite.com/images

mod_rewrite rule error when using /

I have the following RewriteRule in my .htaccess file:
RewriteRule ^advanced-lift-truck?$ pub-listing-full.php?mag=1 [NC,L]
How could I modify this so that .../advanced-lift-truck/ works, as well as .../advanced-lift-truck? (Note the / in the first version.)
I'm assuming I'll need a separate rule, but I've tried the following also:
RewriteRule ^advanced-lift-truck/?$ pub-listing-full.php?mag=1 [NC,L]
but this forwards me to pub-listing-full.php without the query string, and without any CSS/JS files loaded.
Not sure why the query string is missing, but you need to deal with all of your relative URLs in the links of your page. With the extra /, browsers assume the base URI to resolve any relative links (to stuff like style sheets and scripts) is /advanced-lift-truck/. Try adding this to the header of your pages:
<base href="/" />