.htaccess file on xampp (basedir wrong.. incorrect path for images, css files..) - apache

i got another problem.
i have an own apache server (XAMPP) on my computer. The URL in my browser looks like
http://localhost/pageExample/index.php
i use a .htaccess file to change my url from ?action=home to home.html
The problem now is, that the path to all images, css files etc be wrong. They looks like
http://localhost/images/logo.jpg
I think, there is something wrong in my .htaccess file... i tried <base href="http://'.$_SERVER['HTTP_HOST'].'/pageExample/">.. this works ..okay... but some scripts with extern images doesnt work.
My .htaccess file looks like:
RewriteEngine On
RewriteRule ^home.html$ /pageExample/index.php?action=home [L]
(By the way... the problem came since I created the .htaccess)
How to change this?
Thanks!

This is a common problem when you start rewriting URLs but website is not coded accordingly. In your HTML/CSS, when you are referring to images/scripts/css resources you most likely have something like this: <img src="images/hello.jpg" />. Bacuse you are rewriting URLs you need to alter the resource URLs as well -- in the above example resource is linked to the path of the HTML file (relative path) -- you need to make the path absolute. For this -- add leading slash before resource references:
change
<img src="images/hello.jpg" />
to
<img src="/images/hello.jpg" />

Related

Friendly URLs via htaccess

I read lots of tutorials and articles about Friendly URLs through htaccess file, but probably I am missing something.. Thus, I have some questions in order to understand..if someone can help me to clear these out, please!
First of all, before adding any rule to htaccess file,
1. Should I have links like article.php?article_id=1 or the friendly urls I want inside into my files, like: 2016/05/article_name ???
2. I found I link here through another post, that gives a web tool in order to give the rule of the htaccess file: http://www.generateit.net/mod-rewrite/index.php
So, I placed my actual 'ugly' link and gave me right rule to write in htaccess file.
Now, I have uploaded the htaccess file and I am navigating to my website.. If I have the 'ugly' links I can see everything correctly! While I am pressing the friendly url manually there was to issues:
First, I could not see the website's css and javascripts files.. I put the absolute path according to an article, so I guess I am fine with that (is that correct?).
Second, If I press a link let's say the logo, in order to go back to home page, it keeps the rule of htaccess and navigate me to "not found page".. cause the link is something like that: mywebsite.gr/2016/05/index.php, but year and month does not actually exists ...
I would like to have a rule in order to keep the same articles links (I do not have problems about the other links, categories, menus and so on.. just the links of articles). The link I have now is (after all I see and understood):
article.php?article_id=1&year=2016&month=05&name=test-1
and want to have:
mysite.gr/2016/05/test-1.html
*I guess I should mention that the Domain NS does not change yet, and I am working through the temporary link that host provides me.. something like: linux.41.24.23.4.server.gr ... and so on
thanks a lot in advance!! (and sorry for the possible duplicate..)
I think I found a solution... not sure if it is the best one..
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{2})/([a-z0-9\-]+)\/?.html$ category.php?file=$1&year=$2&month=$3&art_name=$4 [NC,L]
</IfModule>
in order to show my links this way:
mydomain.com.gr/2016/06/this-is-an-article.html
of course inside my website, I slit the article name coming from my DB, in order to add the middle slashes and replace any other symbol with 'nothing':
$art_name = "this is an article!";
$art_name = str_replace('!','',$art_name);
$art_name = str_replace(',','',$art_name);
$art_name = str_replace(' ','-',$art_name);
The final link inside my website is:
click me
Furthermore, about the other links: CSS - Js - a tags and so on, as long as I understood there are some options in order to work properly:
You could use Absolute path, or start your links like this (with forward slash in the beginning):
<link href="**/**main.css" rel="stylesheet" type="text/css" />
it's working like an absolute path I believe, cause the file is seeing the root folder (for example for me is localhost - I am working locally with xampp)
I believe I helped someone who have the same questions..

Shutting down a website using HTaccess does not work

OK, it's very simple but it does not work. I have a wiki site where the root contains an index.php file and the subdirectories contains the content of the wiki (I use PMwiki, so no database is required)
I want to temporarity shutdown the website and make it unaccessible by using an nice HTML page to display the shutdown message. I could rename the index.php file, but the rest of the files in the subfolder will remain accessible.
The first thing that worked but which is not elegant is restricting the whole site with a password in the htaccess using "Require valid-user" and all it's other command. The problem is that I cannot display a nice shutdown message as an HTML file.
Else I tried renaming the index.php file to something else like site.php. Creating a index.html file as a message and using a script like this:
Order Deny, allow
Deny from all
<File "index.html">
Allow from all
</File>
In that case, the index.html file is accessible, but it must be manually typed in the URL, it will not use this file by default. I tried adding DirectoryIndex directive like this
DirectoryIndex index.html
But it still does not work.
So first is there a way to make the user only see 1 page in particular and block everything else.
Second, doing so makes the site unaccessible to me. So is there a way to passords restrict the whole directory structure except for a specific index.html file. So that I could type url/site.php and be able to enter my website using an htaccess password.
Thanks for any help
Just this rule in root .htaccess should be able to handle this:
RewriteEngine On
RewriteBase /
RewriteRule !^shutdown\.html$ shutdown.html [L,NC]
Now you can keep custom HTML content in /shutdown.html. Keep in mind you need to use inline css/js since it will also rewrite css/js requests to /shutdown.html file.

mod_rewrite multiple fake subdirectories

Sorry if this is a really dumb question - I can't find the answer anywhere...
I'm storing my test files in a subdirectory called 'dev', and using the following rewrite rule so that requests to /dev/VAR1/VAR2 will take me to /index.php?page=VAR1/VAR2, and requests to /dev/VAR1/ will take me to /index.php?page=VAR1
(VAR1 and VAR2 are variables, and the folders/subdirectories won't really exist)
RewriteBase /dev
RewriteRule ^([A-Za-z/\-]+)/$ index.php?page=$1
This is doing it fine - my index.php is getting the right variables. The problem is that the server or page (or I don't understand what...) thinks I'm actually in the non-existent folder (or subdirectory) which is VAR1, so all my relative (if that's the right word) addresses for images and links and css and scripts etc
<link rel="stylesheet" type="text/css" href="./sitefiles/mystyles.css" />
all work as if they were /dev/VAR1/sitefiles/mystyles.css - basically, the server thinks it's actually in the 'fake' folder.
Is there a way I can continue to use these relative addresses with my mod_rewrite, or do I have to use 'absolute' addresses with the complete path?
Thanks for any help!
We're using the same stuff with our configuration and rewriting fake aliases to index.php?something=$1 we have to put our CSS files into the /styles/ of the DocumentRoot and don't use relative ./ path use the absolute path like href="/sitefiles/mystyles.css" it should work fine.

.htaccess handling leading forward slash

I have a project which I wish to place in a sub-directory of a web root directory.
So if I had:
/public_html as the web root directory
I want the project to live in:
/public_html/myproject
Now, the project will eventually live in the root folder, so I want to make as few code changes as possible, but during testing it must stay in the sub-directory.
The issue is with the following:
<img src="/images/image1.png" alt="" />
This works fine when the project is in the document root, because the leading forward-slash is doing it's job.
However, when the project is in the sub-directory, the / takes the relative path back to the web root folder.
How can I alter .htaccess to prevent this? I need the leading forward slash to reference the "/myproject" folder instead.
Any ideas on how it can be done?
If you have the apache module mod_rewrite enabled, you could add the following to your .htaccess file in the root folder.
RewriteEngine On
RewriteBase /
RewriteRule ^images(.*) /myproject/images/$1
You can find more information on this here:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

HTML Base Equivalent in .htaccess?

Let's say you have the following setup.
You have a server with the basepath set to the normal webroot.
The server has files in the following structure
/projects
/some-unique-id
/index.html
/images
/some-unique-id
/index.html
/images
Is it possible to have a .htaccess file somehow redirect the paths so if index.html has absolute paths they work.
For example if index.html contains /images/foo.gif can this magically become /projects/some-unique-id/images/foo.gif
The equivalent of the base html tag.
This is part of a CMS deliverable previewing system so I am restricted to not changing the HTML code.
Is there anyway to address this in an .htaccess file?
If each index.html contains <img src="/images/foo.gif"/>, the browser will request that URL and there will be no way for the server to know which page caused the request since the requests will be identical regardless of the originating page.
There is no way to do this with mod_rewrite unless you try to check the Referer header, which would be unreliable at best.
You will have to change your HTML markup to solve this. Using relative URLs seems like it would solve your problem.