I have effectively used the rewrite rule to change my url from /teams.php?team=New York Yankees&year=2012 to teams/New York Yankees/2012.
My .htaccess file contains the following
RewriteEngine On
RewriteRule ^teams/([^/]*)/(^/]*)$ /teams.php?team=$1&year=$2 [L]
However, with this rewrite rule currently in effect, none of the external CSS and JS files will load. I've tried putting the following rewrite condition before the rewrite rule with no luck
RewriteCond %{REQUEST_URI} !^/(css/js)/
Almost all of my files have the following link to the external stylesheet.
<link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
Any ideas what I'm doing wrong here?
Thanks
You will have to use full paths when linking to your stylesheets, not relative paths like you have now. With the rewrite rules in place they become relative to the pre-rewritten URL.
For example, change:
<link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
to:
<link rel="stylesheet" type="text/css" href="/css/stylesheet.css" />
The reason they are not loading is because the browser is trying to access:
http://yoursite.com/teams/New York Yankees/2012/css/stylesheet.css
when you link to css/stylesheet which of course does not exist.
Related
I have to deploy a React/Node application through cPanel for a school assignment, and I am having some issues. If I reload the page, I get the following error:
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I found that adding a .htaccess file will let me refresh the page, but now my API request gets the error of:
Unexpected token '<', "<!doctype "... is not valid JSON. Full response from Postman:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="/favicon.ico" />
<link rel="manifest" href="/manifest.json" />
<title>Decentralized Technologies</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Anonymous+Pro&family=Russo+One&family=Syne:wght#500&display=swap"
rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
<link rel="stylesheet" href="https://unpkg.com/aos#next/dist/aos.css" />
<script defer="defer" src="/static/js/main.3d02b5b2.js"></script>
<link href="/static/css/main.e75f676e.css" rel="stylesheet">
</head>
<body><noscript>You need to enable JavaScript to run this app.</noscript>
<div id="backdrop-hook"></div>
<div id="modal-hook"></div>
<div id="root"></div>
<script src="https://unpkg.com/aos#next/dist/aos.js"></script>
<script>
AOS.init()
</script>
</body>
</html>
.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
There is also an autogenerated .htaccess file in an API folder in my public_html folder within the cPanel finder:
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/<name>/server"
PassengerBaseURI "/api"
PassengerNodejs "/home/<name>/nodevenv/server/10/bin/node"
PassengerAppType node
PassengerStartupFile app.js
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
<keys>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END
I've tried adding the contents .htaccess that I manually created to the autogenerated one and vice-versa, but when both the and the are in the environment at the same (whether in the same file or separate), the API calls do not work. I'm at a loss for how to be able to do both of these things together and am looking for some help.
Thanks!
I was able to get this to work by adding RewriteRule ^(api) - [L] right after RewriteEngine On. The ^(api) tells the redirect to ignore rewriting all calls to that have api in the url.
I have 675 html files that ahve been passed to me today.
The folder structure is as follows:
- index.html
- /assets/
- /pages/
In the pages/ folder I have the 675 html files. In each of them I have all the assets that are relative to the root folder, like so:
<link rel="stylesheet" href="/assets/css/main.css">
I have uploaded everything to my demo server, and the URL is like so:
https://example.com/CLIENT/rebrand/
The index.html page contains a list of the 675 html files in pages folder.
As you can imagine, when I click on one of them, I get the page, but images, js and css are not loaded.
Is there a way to instruct the server, via .htaccess maybe, to interpret
<link rel="stylesheet" href="/assets/css/main.css">
as
<link rel="stylesheet" href="/CLIENT/rebrand/assets/css/main.css">
I would like to avoid a massive search&replace, in so many files, because the risk is high to break something.
Do you guys have any hint on a cleaner solution? Thank you
You can use this redirect rule at the top of your .htaccess for this redirect:
RewriteEngine On
RewriteRule ^assets/.+$ /CLIENT/rebrand/$0 [L,NC,R=301]
# other rules appear below this line
I have a subdomain http://www.sub.example.com but the index file is not in the root directory but instead it is project/example/index.html and some of it's dependencies are there and in the parent directory project/. How can I make it so that users visiting http://www.sub.example.com are able to access the website?
I have tried setting a DirectoryIndex like so:
DirectoryIndex project/example/index.html
But all of my images, scripts, and styles return a 404.
So I tried to rewrite like so:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/project/example/
RewriteRule ^(.*)$ /project/example/$1 [NC,L]
Which fixes my styles and images (which are in projects/example/ directory). However, all of my other dependencies in the parent project/ directory are still missing. Is there a way to include them?
Example index.html dependency in parent directory:
<script src="../node_modules/angular/angular.js"></script>
<link href="styles.css" rel="stylesheet"/>
Add this just below <head> tag of your page's HTML:
<base href="/project/example/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
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" />
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>