htaccess rewrite rule not loading site content - apache

I am struggling with .htaccess rewrite rules.
Let's say I have this URL.
localhost/site/index.php
and I want to rewrite it as this URL
localhost/site/tutorial
I would use this RewriteRule
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^tutorial/(.*)$ /up/index.php
The page works, but the CSS files don't load.
Also, if I have a URL like this:
index.php?page=home
Then I would have to parse through that URL to get 'home' not using $_GET anymore correct??

Just use absolute URLs for your CSS file, or at least reference from domain root.
http://www.mysite.com/css/myCssFile.css
or
/css/myCssFile.css

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^tutorial/(.*)$ /up/index.php
This will rewrite anything that goes to the tutorial directory as /up/index.php, which means that unless you host your CSS file at that location, it won't work. If your css file is at /tutorial/css.css, it will redirect it to /up/index.php
You would have to parse through the %QUERYSTRING% in order to redirect based on index.php?page=home

So the CSS files are not loading because you are changing the location of the served file.
So if you are linking to ./css/style.css => ^/tutorial/css/style.css is not the same as /up/css/style.css
And you can retain the get if you rewrite:
RewriteRule ^tutorial/(.*)?(.*)$ /up/index.php?$2

Related

URL rewriting with .htaccess and relative paths

I have a small problem with rewriting urls. I have this really simple rewrite rule:
RewriteEngine on
RewriteRule ^(home|apps|news|research|contribute)/$ index.php?page=$1
However, whenever I call my website like this, e.g. website.net/home/ it correctly rewrites the rule and shows the corresponding page but also messes up the css and images since it rewrites the urls for them since they are relative paths in the code.
Is there any easy way to prevent this from happening?
This is because of the rewritten urls. when the url is example.com/home/ apache thinks /home/ is a directory and appends /home/ in front of all relative urls.
To solve this, You can add the following base tag in head section of your webpage :
<base href="/">
Related : Seo Friendly Url css img js not working
Add this following line in your .htaccess file
RewriteRule \.(gif|jpg|css|js|inc\.php)$ -[L]

Clean URL not working properly

Hey I'm struggling with this all day long and I'm new to clean URLs.
My Domain which i want to change:
http://www.domain.tld/ausflug.php?lang=de
to:
http://www.domain.tld/ausflug/lang/de/
via .htaccess. This is my code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ausflug/lang/(.*)/ ausflug.php?lang=$1
My Problems are:
If I navigate through my page, the url still is the ugly one.
And if I want to reach the page with the clean url, it won't display all the
css and js stuff.
Thanks for help!
First of all, replace your current code by this one (your htaccess should be in root folder)
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/ausflug\.php\?lang=([a-z]{2}) [NC]
RewriteRule . /ausflug/lang/%1/? [R=301,L]
RewriteRule ^ausflug/lang/([a-z]{2})/$ /ausflug.php?lang=$1 [L]
This will redirect domain.tld/ausflug.php?lang=de to domain.tld/ausflug/lang/de/ and will internally rewrite it (without any redirection loop error) to domain.tld/ausflug.php?lang=de.
For your second problem:
if I want to reach the page with the clean url, it won't display all
the css and js stuff
Simply use absolute paths for your links (with a leading slash).
Example: /css/file.css instead of css/file.css (otherwise it will look in wrong directory, since your rule is generating some virtual directories)
Or you can add this html tag <base href="/"> right after <head> in all pages (instead of replacing each links)

rewrite engine is removing slashes from my urls

I have looked everywhere and spent hours trying to get this working, but I have been unsuccessful. I could really use some help.
I am not an expert with apache but I have an old URL and am trying to redirect all my old pages to my new URL. My old URL is:
www.bc-mortgage-brokers.ca
My new URL is
http://bc-mortgage-broker.ca
The home page redirects correctly, but it is the only one to do so. For example, if I type:
www.bc-mortgage-brokers.ca/16/
into my browswer, it removes the slashes and then fails to redirect. I will get this url instead:
bc-mortgage-broker.ca16
in my .htaccess file, I have the entry for this particular one as:
redirect 301 /16/ http://bc-mortgage-broker.ca/16/
My header for the file is currently:
Options +FollowSymLinks
RewriteEngine on
I am creating the file in textmate.
When I look at the error logs, I read:
RewriteCond: bad flag delimiters
Use this rule in your root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(16(?:/.*)?)$ http://bc-mortgage-broker.ca/$1 [L,R=301]

htaccess rule without noticable redirect

i have the following redirect rule
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^$ /folder/
basically my index.html file is in folder.html and instead of copy and pasting the index.html file from /folder/ i was trying to use the rule above to request the file and output the file as mysite.com but instead it is redirecting to mysite.com/folder/index.html. I assume this is possible to do as frameworks use the rewrite rule and this doesn't redirect the browser. Can anyone advise on what i am doing wrong?
Thanks in advance.

Redirect based on part of url

How do I do an apache redirect to a startic splash page based on a dynamic url. Part of the url is always static. For example, http://buy.domain.com/product/product1-name-here
What do I need to do if I want to redirected everything that comes in http://buy.domain.com/product/* to a certain page?
Just try this simple rule in .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteRule ^product/.*$ /yourSplashPage? [R,L,NC]
In your .htaccess file, create an Apache RewriteRule to selectively choose links based on a regular expression.
RewriteRule ^product/.*$ /certainPage.html?