.htaccess rewrite without it affecting relative image/css/js URLs? - .htaccess

I need some simple help with my htaccess file, thank you in advance.
I have a number website URLs, such as:
www.site.com/index.php?page_path=solutions-overview.html
www.site.com/index.php?page_path=solutions-a.html
I want to use the RewriteEngine to change the above links to the following respectively:
www.site.com/solutions/overview
www.site.com/solutions/a
Below is the .htaccess code I am using:
RewriteEngine On
RewriteRule ^solutions/overview index.php?page_path=solutions-overview.html
RewriteRule ^solutions/a index.php?page_path=solutions-a.html
This works, however all of my images, CSS files and JS files no longer load because the page is trying to fetch the wrong URLs. For example "/images/blah.jpg" is instead loading as "/solutions/images/blah.jpg".
How can I modify the htaccess code to prevent relative URLs from changing?
Thank you!

Add this line <BASE href="http://www.yoursitename.com/"> to your page inside the <head> tag as follows:
<head>
<title>Your site title</title>
<BASE href="http://www.yoursitename.com/">
....
</head>

Related

Remove backslash and slash after file extension in htaccess

Htaccess is not my favourite and please be kind enough to help me with this.
Ex. Www.domain.com/about.php shows the site with styles rightly. In the browser if add www.domain.com/about.php/ or www.domain.com/about.php\ all the styles are ruined and page is distorted.
How to eradicate backslash and slash after file extension in htaccess.
Appreciate your help
Rgds
It is path/to/style.css but when you add / after about.php , the included file path will be changed to about.php/path/to/style.css so nothing will show up.
To solve this issue add this <base href="/"> between <head> Tags in order to keep a base fixed :
<head>
<base href="/">
</head>
Update:
As per your comment that you want to add rules in .htaccess for this case , try this :
RewriteEngine On
RewriteRule ^(.+)\.php/(.*)(\.css)$ $2$3 [L]

why is my RewriteRule not working

I want to be able to Rewrite a following url:
www.mysite.com/en/contact
to something like this
www.mysite.com?l=en&p=contact
but keep the structure.
I tried the following RewriteRule:
RewriteEngine On
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?l=$1&p=$2 [L]
but the browser then wants to load all the included scripts like the css from
www.mysite.com/en
How can I tell the browser, that it shouldn't follow the url link?
Or something like that
Thank you for your help
Chris
There are 2 methods to solve this:
1) Use base tag inside head tag
<base href='/'>
2)Use relative links
<link rel='stylesheet' href='/css/style.css'>
Here the browser looks for the CSS file in the folder in the root named CSS, whatever the URL request may be.

How to rewrite certain pages but not CSS, JS etc?

I've been trying for at least 4 hours to fix this problem on my own and have scoured through several hundreds of Stack Overflow pages and other forums in search of a solution, but I can't seem to solve this simple problem.
The contents are arranged as follows:
-- index.php
-- js
- script.js
-- css
- style.css
-- images
- image.png
In the website, I needindex.php?dept=foo to be rewritten as dept/foo. This works with the following RewriteRule:
RewriteRule ^dept/(\w+)/?$ index.php?dept=$1 [NC]
But then the CSS, JS and images won't load any more.
The CSS and JS files are currently are currently included as follows:
<link rel="stylesheet" href="css/style.css" />
I tried changing the URLs to have absolute paths (i.e. /css/style.css too`, but that didn't fix it either).
How can I correctly fix the problem?
Based on your comment have this rewrite rule:
RewriteEngine On
RewriteRule ^dept/(\w+)/?$ index.php?dept=$1 [NC,L,QSA]
Then add this just below <head> section of your page's HTML:
<base href="/live/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
You can keep your CSS path as:
<link rel="stylesheet" href="css/style.css" />

URL rewrinting from www.example.com to www.example.com/beta

His friends, currently my website is set up in the "beta" folder in my root just like
www.example.com/beta
Now if any user enters the following URL
www.example.com
then he should be automatically redirected to the first URL, any ideas? I know this can be done through .htaccess but i am not sure about it :( help me please. thanks
You'll find a good example of what you want right up at the top of When Not To Use Rewrite, which is a page everyone should read before they edit one character of an .htaccess file.
RewriteRule ^/(.*) http://other.example.com/$1
# is better expressed as..
Redirect / http://other.example.com/
Or, in your case,
Redirect / /beta/
Try adding
Redirect 301 / http://www.example.com/beta
to your .htaccess file in the root directory (untested)
Actually you can do it pretty simple (no .htaccess modification required).
Edit the headers of index.html file from your server main director (/).
Within the <HEAD> ... </HEAD> section add your redirection code, for example:
<meta http-equiv="refresh" content="0;URL=http://www.example.com/beta/somepage.html" />
Note that this should be located inside index.html of you main dir /.
For references see: http://devmain.blogspot.co.uk/2013/06/html-auto-redirect-website-to-different.html
Hope it help.

Relative paths break in HTML when using mod_rewrite in .htaccess

I am new to .htaccess and mod_rewrite.
I am using the following rewrite rules:
RewriteRule ^wants/([0-9]+)$ /wantlist.php?wantid=$1
RewriteRule ^dashboard$ /dashboard.php
I am able to successfully redirect both the cases.
But in order to do this, I had to change the CSS, JS and other paths to absolute paths without which the CSS and JS did not load.
I was using
<script type="javascript" src="js/javascript.js"></script>
which after the rewrite rule did not work and after I changed it to
<script type="javascript" src="http://example.com/js/javascript.js"></script>
I got it to work.
The same is happening for any of the other URLs on the website.
Is there a way to bypass this as I am using relative paths almost everywhere in the site I am developing?
This is an URL resolving issue. See mod_rewrite URL info required
You could explicitly set a base URI with the BASE HTML element, for example:
<base href="/">

Resources