url rewriting is not working for relative paths - apache

I am a newbie for rewriting url, I have rewritten my url, but it is causing problem for all the relative paths used in the page like
<link href="style/style.css" rel="stylesheet">
<link href="images/icon.png" type="images/ico" rel="icon" />
<img src="images/test.png" id="test">
Even i have applied the following rewrite rule for them, but still i find 404 error in firebug console (first one is working but second is not working for relatvie)
RewriteEngine On
RewriteRule ^raipur/([A-Za-z0-9-]+)/([0-9]+)$ /viewRestaurant.php?raipur=$1&id=$2
RewriteRule ^raipur/([A-Za-z0-9-]+)/([A-za-z]+)/ /$2/
my console screenshot
I had even debugged my rewrite rule into htaccess tester and its working there as required

You links are all relative links. "images/icon.png" instead of "/images/icon.png". Because your url changed its URL base from:
/viewRestuarant.php
Base: /
to
/raipur/something/1234
Base: /raipur/something/
When the browser sees a link like: images/icon.png it needs to prepend a base URL to it in order to know where the resource is located. By default it uses the host and base based off of the URL that it sees in the location bar. Since that's obviously not where any of these resources are, you need to either make your links into absolute URL's like: /images/icon.png or http://example.com/images/icon.png or add an explicit relative URL base into the header of your pages (between the <head> </head> tags):
<base href="/" />

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.

What HTTP header indicates when Index.html has been served?

Excuse the awkward title: I'm building a simple web server (don't ask...) and have this problem:
The browser requests mydomain.com/MyFolder
My server spots this is a folder, so instead, delivers mydomain.com/MyFolder/index.html
All fine so far, except that index.html has link to mycss.css, but the browser requests it as a top-level file mydomain.com/mycss.css instead of mydomain.com/myFolder/mycss.css.
Is there some HTTP header that needs setting up to indicate that a different page has been served? I've tried returning Content-Location: /myFolder/index.html, but without any visible success.
index.html basically contains this:
<link rel="stylesheet" href="mycss.css" />
Return a 301 Moved Permanently status code, instead of the 200.
Provide a Location header pointing to the same url plus a slash in the end /
Like so:
Location: mydomain.com/MyFolder/
Do not serve the index.html file on that same request, wait for the browser to request again with the slash at the end.
Or just try to add something like this to your .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)$ myFolder/$1
Can you just try to change this:
<link rel="stylesheet" href="mycss.css" />
to this:
<link rel="stylesheet" href="/myFolder/mycss.css" />
?
Ok, actually the information given was not correct so I'm modifying the answer. If you are implementing a web-server, you must follow the standard specification. The definition <link rel="stylesheet" href="mycss.css" /> shall retrieve the the CSS file from the same location as the file in which it is defined in (index.html) is. When using relative paths, it is not the browser that requests from a specific location but the web-server should determine the location from which to serve the resource.
Check section 2.4.6 and 3 in the standards document: http://www.ietf.org/rfc/rfc1808.txt
In other words, if the path of a resource does not start with the slash (/), it is considered as relative and should be located relative to the base URL.

Subdomain redirect with htaccess without changing URL in the address bar

I set up a subdomain on my web host like this:
en.domain.com pointing to the folder /en/
But when entering "en.domain.com" in the address bar, the URL changes to
domain.com/en/
And if I navigate further, let's say to folder "aaa", the URL turns into
domain.com/en/aaa/
Is there a way to make the subdomain stay in the address bar, like this?:
en.domain.com/aaa/
I tried everything and nobody could help me. After much research, I found this and it works for me. So here my own answer, that may help others searching for the same thing.
This will make that the URL showing the subdomain ("en.domain.com") doesn't change in the address bar and even if someone enters "domain.com/en/" it will rewrite the URL to "en.domain.com":
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^en(/(.*))? http://en.domain.com/$2 [QSA,L,R=301]
This will break the paths of your site, causing that styles and images won't show. Therefore you need to put this in your HTML code on every page of your site, according to the location of each page in the structure of your site:
For the page in folder "en":
<head>
<base href="http://domain.com/en/" />
</head>
For the page in folder "aaa":
<head>
<base href="http://domain.com/en/aaa/" />
</head>
For the page in folder "bbb":
<head>
<base href="http://domain.com/en/aaa/bbb/" />
</head>
You are welcome! :-)