Redirecting a wordpress blog - php

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.

Related

How to stop accessing the user from specific URL scheme

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

URL path with universal header is not working

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

Redirect to/Show the 404 page in root directory when page/folder not found

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.

.htaccess redirect subdirectory as query string in php

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.

How Do I Set up my homepage?

So I have developed a website with the main page named as home.php. I have also got a domain name and hosting from Godaddy. What I want to know is that how do i tell my site to display the home.php as the homepage?
I want that when the user goes to example.com -- home.php should be presented, and not example.com/home.php.
thanks.
just rename your home.php to index.php
Name the file index.php and it will work
You're options:
Rename you're file to index.php
Rename you're file to default.php
Do a iFrame
Do a php redirect: header("Location: home.php");
You could do a .htaccess redirect: DirectoryIndex home.php
I would choose #1 or #2!
If you choose to do an iFrame you would have to "break out" to enable the back button in the browser.
If you choose to do a redirect with php you will waste server resources.
.htaccess way: ...Don't know what's bad.
you can:
rename home.php to index.php
create index.php with content
<?php header('Location: home.php');?>
But, I don't recommend option #2 as it generate another useless HTTP request.

Categories