Problem with htaccess when making friendly urls with the trailing slash does not work [duplicate] - apache

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.

Related

redirecting about.php to domain.com/About/ using .htaccess rewrite rules

When I write RewriteRule About About.php [L,QSA] in .htaccess to make users access example.com/About.php using the following link example.com/About/. Users can access it correctly if it was written as example.com/About only. If I added more backslack like this example.com/About/ the CSS files are not loaded. Also, if anyone accessed this link example.com/About/SomeText he didn't get error 404 he gets about.php page without loading CSS as well.
RewriteRule About About.php [L,QSA]
The problem with this rule is that it rewrites any URL that simply contains the word "About". So, /About/SomeText, /AnythingAbout and /AbcAboutXyz are all rewritten to About.php which processes the request. (It also rewrites itself, which is another matter!)
You need to be specific and match only requests for /About exactly. For example:
RewriteRule ^About$ About.php [L]
The first argument to the RewriteRule directive is a regex that matches against the requested URL-path (less the slash prefix).
The QSA flag is not required here.
If I added more backslack like this domain.com/About/ the CSS files are not loaded.
"backslack" - that is a (forward) "slash" (and not a "backslash" either).
This will happen if you are using relative URL-paths to access your CSS files (and other static resources like images and JS). The relative URL-path is resolved by the browser relative to the current URL. eg. If you are linking to your stylesheet with href="styles.css" and the current URL in the browser is /About/SomeText then the browser will naturally request /About/styles.css, not /styles.css as you might be expecting (when the URL is simply /About).
When rewriting the URL at different path depths you need to use either:
Root-relative (starting with a slash) URL-paths.
Absolute URLs (with scheme + hostname).
Specify a base element in the head section of the HTML source that tells the browser what URL should be used as the base for all relative URLs. Although there are caveats with this approach (as mentioned in the linked question below)
(Although, in restricting the above rule to match /About only then this naturally avoids this issue.)
See also my answer to the following question that goes into more detail on this:
JS and CSS Rendering Issues After .htaccess File URL Rewrite Rule

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.

My rewritten url redirecting but to a website without css or any other rule being applied

This is my original url
http://havemybooks.com/blog_single?blog_no=39&blog=Ten%20quotes%20by%20national%20leader%20to%20make%20this%20independence%20day%20more%20speacial.
This is my rewritten url
http://havemybooks.com/blog_no/39/blog/Ten%20quotes%20by%20national%20leader%20to%20make%20this%20independence%20day%20more%20speacial.
Here is the code I'm using in .htaccess. I don't know a much about .htaccess so I'm using htaccess generator tool.
RewriteEngine On
RewriteRule ^blog_no/([^/]*)/blog/([^/]*)$ /blog_single?blog_no=$1&blog=$2 [L]
This is the error I get when I inspect elements of my website.
Resource interpreted as Stylesheet but transferred with MIME type text/html
As Mike commented above, you just need to load the static content from site root. Current, all your URLs are being rendered relative to the page you are visiting.
Add a base tag in the <head> of you pages, and the content will load as required.
<base href="www.example.com/">

how to rewrite url with htaccess and keep relative links working

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.