I was working on a Project & I added 404 Page not Found code in htaccess file of it.
This 404 Error Code working fine for all type of urls if someone accidentally type the wrong name of page etc.
But, I try to add the same name of page 2 times in the URL path and it show me something very strange.
Lets suppose i have a website http://example.com
It has a page named dashboard.php, So I first try to access it using http://example.com/dashboard.php. It show me the dashboard.php as normally it should be show me.
Issue:
When I try to access this dashboard.php using http://example.com/dashboard.php/dashboard.php, It show me all the texual content without including css,js,images files.
Screenshots:
http://example.com/dashboard.php
http://example.com/dashboard.php/dashboard.php
My htaccess code (Working fine):
ErrorDocument 404 http://example.com
In your error page, use only absolute links (with domain, or from root with / in the beginning), especially for css or images.
Or you can add in the header:
<base href="http://example.com/">
or
<base href="/">
You can stop the user from accessing url like this with htaccess :
AcceptPathInfo Off
https://httpd.apache.org/docs/2.4/en/mod/core.html
Related
I have a wordpress installation that is located in the root folder: mydomain.com
This website has a blog that is directed to mydomain.com/blog
What I would like to achieve is to redirect all traffic that goes to mydomain.com/blog to another wordpress installation that has a blog and located on a temporary domain on my server - server.myserverdomain.com/~seconsite/blog
I would like to also keep the main website domain mydomain.com/blog
Thanks !
Redirect by PHP
header("Location: server.myserverdomain.com/~seconsite/blog");
die();
Redirect by HTML
Method 1 :
<meta http-equiv="refresh" content="0; url=http://server.myserverdomain.com/~seconsite/blog/" />
Method 2 :
<script>window.top.location = 'http://server.myserverdomain.com/~seconsite/blog';</script>
What you can do is
Redirection by JavaScript
Make one file named index.html, in which put html,head and body tags and also put javascript code as below
<script>
window.location="http://UrlToRedirect";
</script>
Put this index.html file into your /www/blog directory or whichever directory you have for mydomain.com/blog.
And make sure you don't have any other file named as index in that directory except this one which you will prepare.
And you are good to go.
I have three pages on my localhost 1 is my index page,2 is my universal header page which is under includes folder of and 3 is my html file which is under html folder.The header file is included in both the index file and html file like that...
for index.php-include("includes/header.php");
for html.php-include("../includes/header.php");
and my header has the link of index.php page that is (./index.php)
Now my questions is that when i open my index page and click on link of index page from my header it takes me to same index.php page but when in open html.php page and then click index.php page link from header it does not go to index.php page but it goes to this page-
(localhost/educational%20website/html/index.php) how to solve that.
And i also want to know that write now i am on localhost but when i make my site live is there any need to change the paths because i am making around 150 pages with your technique plaese so please answer me that kind of technique that is used for both localhost and on live
Your are including paths relatively, use a (absolute) base path in your index.php to fix this:
include_once($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
One way is to define a variable or a constant for the site's url in the header.php file. Then in all your other pages, you could just use this variable/constant when you need to mention the other urls.
Eg(put this as first line in your header.php file):
define('SITE_URL', 'http://localhost/educationalwebsite');
Here, we have defined a constant named SITE_URL. Then in other pages, you are already including this header file. Isn't it? So, this constant will be available in your index.php, html.php and other pages.
And suppose for a link in your html.php file(to point to the index.php), you could use it like this:
Home
If you want to include link to the html.php file residing inside html folder, it would be like:
HTML
By using this way, if you are uploading the whole site to a live server, you only need to change one line, ie. the first line in header.php, where we have defined the SITE_URL constant. Just change it's value to the new URL of the home directory of your website.
Hope this helps
OK guys, I have a problem that is literally driving me crazy. Here's what happened :
I decided that I wanted to rewrite the URL on my web-site.
It is supposed to rewrite from this syntax :
http://www.sample.com/programming.php?name=something
to this :
http://www.sample.com/tutorials/programming/something.php
Or (eg. 2) :
http://www.sample.com/other.php?name=test
to this :
http://www.sample.com/tutorials/other/test.php
So my URL syntax would be :
http://www.sample.com/tutorials/(name of my file)/(name of the variable).php
I have tried the following code :
RewriteEngine On
RewriteRule ^tutorials/programming/(.+)$ /programming.php?name=$1 [L]
RewriteRule ^tutorials/other/(.+)$ /other.php?name=$1 [L]
But, it doesn't rewrite the URL properly. The detailed explanation is below :
So, when I visit my original site, it appears like this :
http://www.sample.com/programming.php?name=something
If I visit this URL :
http://www.sample.com/tutorials/programming/something.php
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
http://www.something.com/index.php (Correct index.php URL)
but it's like this :
http://www.sample.com/tutorials/programming/index.php (which does not exist).
I have read over 10 tutorials online, asked my colleague to help me out, but neither did his solutions work. So, all I want to accomplish is that my URL is rewritten, so when the user choose a tutorial in programming, I don't get this URL in the address bar :
http://www.sample.com/programming.php?name=something
but this :
http://www.sample.com/tutorials/programming/something.php
and that is all I want.
I have tried to be as detailed as possible. If you need additional details, please, let me know.
Thanks in advance!
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
The relative/absolute paths you have in your page content is getting a different base because of the extra slash. When you have something like <img src="images/blah.gif">, the relative base is derived from the URL the browser sees (not what is internally re-written by the server). So if the browser sees: http://www.sample.com/programming.php?name=something, the URI is /programming.php and the URI base is /. But if the browser sees http://www.sample.com/tutorials/programming/something.php, the URI is /tutorials/programming/something.php and the URI base becomes /tutorials/programming/, which I'm assuming is not where your images/css/scripts/etc are located (since that directory probably doesn't even exist).
You need to either correct the URI base in all of your page headers by adding a:
<base href="/">
Or change all of your relative links to absolute links.
I get my web-site HTML, but without my CSS layout (just HTML displayed)
how did you include the css? did you include your files in the way
../../css/file.css
or in absolute mode
/css/file.css?
to check what really is the fault (i guess your rewrite success in your task eg i didn't understand it the right way) can you give us the real uri's?
Please consider the following structure of my website:
www.mydomain.com/
www.mydomain.com/index.php
www.mydomain.com/404.php
www.mydomain.com/css
www.mydomain.com/blog/
How can I show the 404.php page that exists in my root directory along with its stylinf and css, in the following cases:
When user tries to access a non-existing folder and/or its contents?
Example: www.mydomain.com/nofolder
When user tries to access nested non-existing folders?
Example: www.mydomain.com/nofolder1/nofolder2/nofolder3/
When user tries to access non-existing file nested non-existing folders?
Example: www.mydomain.com/nofolder1/nofolder2/index.php
I already have a .htaccess file that shows the 404 page when a non-existent page is accessed in the root folder of my website. My current .htaccess file:
ErrorDocument 404 /404.php
The above code shows the CSS stripped version of my 404.php page when a non-existent folder (Example: www.mydomain.com/nofolder) is accessed. I need to show all the CSS and styling intact. For user friendliness, I think its best to retain the URL that was entered in as it is, but still show the 404 page. How can this be done?
EDIT 1:
I need to show this without using absolute links for the CSS. How can I do this?
The above code shows the CSS stripped version of my 404.php
You must use absolute path for the CSS link. Instead of this:
<link rel="stylesheet" type="text/css" href="css" />
use this:
<link rel="stylesheet" type="text/css" href="/css" />
Because the Error 404 page has to be able to run from any URL my 404.php simply contains
<?php
header("Location: http:/homepage.de");
?>
This way it relocates to the base URL.
This is hard to explain, so hopefully I'm understood in my question.
(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1
This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.
(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:
switch ($_REQUEST['page']) {
default:
include ("home.php");
break;
case "apps":
include ("apps.php");
break;
}
There seems to be no problems so far.
(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.
So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.php file being treated as if it were in the /apps/ directory?
First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.
I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:
$base_url = 'http://www.mywebsite.com/';
The config file should be included in index.php unconditionally.
So, when you include any js or images, you do it like this:
<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />
<img src="<?php echo $base_url;?>images/gradient_green.jpg" />
If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.
Another possible solution is to use the base tag. Look it up here:
http://www.w3schools.com/TAGS/tag_base.asp
I hope this helps.
use absolute urls for js, css and images on your pages (starting with a slash).
/js/main.js instead of js/main.js
You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.
The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.
So the options you have are:
Do an external redirect with your .htaccess (and defeat your original purpose ;-)
Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
Use absolute URLs, just as #nobody has suggested in his answer.
The last one is the only real option IMHO.