How to autoload a site through a php file? - php

Is there a way to use a php file to automatically load a website.
I have a new version of my website, but I want to keep the current version live (with its current domain). I would just like to direct clients/customers to preview the new version but still be able to go to the current site.
For example: They would go to the current site by visiting www.currentsite.com. And to visit the new version they would visit www.currentsite.com/newversion
Both sites are subfolders within the root of my hosting account. How do I achieve this?
Thanks for any help on this question.

You probably want to take a look at .htaccess files:
Assuming your current website is located in /home/user/public_html/current and your new website is in /home/user/public_html/new, you could set up something like this:
RewriteEngine On
# Rewrite every uri that does not start with /newversion/ to the current website
RewriteCond %{REQUEST_URI} !^/newversion/?(.*)$
RewriteRule (.+) /current/$1 [NC,L]
# Rewrite everything else to /new
RewriteRule (.+) /new/$1 [NC,L]
Judging from your post and the fact that you are mentioning multiple folders in a hosting account, there is a pretty good chance you already have something like this setup for your current website.
Take a look in your root folder and see if there is a .htaccess file and start editing away.

If you're looking for a PHP-only solution, you can put an index.php or simmilar file at the root folder of the domain containing:
<?php
header("Location: http://www.currentsite.com/current");
?>
That would enable a redirection from http://www.currentsite.com/ to http://www.currentsite.com/current. The drawback is that you'd have to stick with "current" in your path, so I'd go for the more elegant .htaccess solution.
EDIT
Oh, and if you already have an .htaccess file in the root folder doing similar rewriting tasks, chances are this approach won't work as index.php won't ever be reached. You'd better check for that first.

Or create a simple index.html file in the "/newversion" directory with an iframe.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<iframe src="/" width="100%" height="100%"></iframe>
</body>
</html>

Related

PHP - make url smaller but with the same functionality

I've got a CMS which I created by myself and I have an external link, which shows the web created by the user.
The link is: http://localhost/CMS/www/users_template/{user_name}
Is there any possibility to change the link and make it:
http://localhost/CMS/{user_name}
but it should still show the web page without any errors?
I think I should edit my .htaccess file, but no idea how to do it. I have never edited the .htaccess file before.
This is a duplicate.
Please view .htaccess: removing part of url path and stay at base
RewriteEngine On
RewriteBase /CMS/
RewriteRule ^www/users_template/(.*)$ $1 [L,R=301,QSA]

Redirecting from an old site to a new site

I have a very peculiar problem. Formerly, I was using Wordpress and I have a link that has a path of http://www.acetraining.com.sg/index.php/contact-us/.
I have just reverted the website to a non-Wordpress and have a static link of http://www.acetraining.com.sg/contact_us.html. How is it possible for me to redirect the old path to this new path? I already have a redirector script and I know I have to place this code :
<meta http-equiv="refresh" content="0; url=http://example.com/" />
to the head region of a html page. What I have done is that I've created an index.php as a folder name and another contact-us as a subfolder and then I created an index.html file within that but it does not work at all.
Any suggestions anyone?
Good morning!
I think that using meta tag for redirection will not good for your SEO. As it is a definite URL moving, I suggest you the header 301 (Redirect Permanent). You can use this by creating a .htaccess file in public_html (or your website public root directory) and put on something like this :
RedirectPermanent /index.php/contact-us/ http://www.acetraining.com.sg/contact_us.html
Hope this may help you.
If you don't have a .htaccess file in your server root!
Create an empty text file using a text editor such as notepad, and save it as htaccess.txt and add the code blow.
Options +FollowSymLinks
RewriteEngine On
# Redirect old file path to new file path
Redirect index.php/contact-us http://www.acetraining.com.sg/contact_us.html
NOTE:
The reason you should save the file as htaccess.txt is because many operating systems and FTP applications are unable to read or view .htaccess files by default. Once uploaded to the server you can rename the file to .htaccess
hopefully it will fix the redirection for you.

Php includes and images not working for Rewritten urls

After editing htaccess file for the short urls ,now my page has broken and php included files and images are not working,
This is my htaccess in the public_html
RewriteRule ^car/name/(.*)$ /car.php?name=$1 [L,QSA]
All included files and images are in /files folder ,
Now the problem is when I visit
http://example.com/car.php?name=hundai
it works and everything is ok,
but the problem is with Rewritten url
http://example.com/car/name/hundai
It breaks all php files and images.
Can you please help a fix for this?
RewriteRule is changing your URL http://example.com/car.php?name=hundai into http://example.com/car/name/hundai ,which means that files path is inside a directory it was not before. this means that any relative links in document will not work. To solve this You need to use an Html
Base
tag inside the "head" of your document.
<head>
<base href="http://example.com/files/">
</head>

htaccess url rewrite rule not working as expected

I have a complex problem that I an unable to solve for days now. Maybe some expert with more knowledge of htaccess functionality will be able to help out.
I have two files placed in the root directory - test.php and files_include.php.
The URL that a user would normally see is:
www.example.com/test.php?cs1=A&cs2=B&cs3=C&cs4=D
Since this is a ugly URL I would like to rewrite it to something better like:
www.example.com/search/A-B-C-D.html
Using a rule in .htaccess like this I can easily rewrite the URL:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /test.php?cs1=$1&cs2=$2&cs3=$3&cs4=$4 [L]
In the file test.php I call for the website config files like this:
include('files_include.php');
Now the problem. As soon as I rewrite the URL to a location different from the root one, I get a really strange issue. The page still renders correct in browser but:
Problem 1. I have to replace src="images with src="../images if I want to see the image correct. This can be easily corrected by giving an absolute link, it is the easier part to do.
But the question is why is the relative path changing? Is .htaccess making the browser think we are in search'/ folder? The answer to this question will help me to identify the main issue, which is Problem2.
Problem 2. Sitemaps generators cannot follow the links on the page once the URL is rewritten, as if it appears blank to them, no matter that in browser all looks fine.
Therefore I am guessing that by rewriting the URL to search/A-B-C-D.html I am breaking something with the inclusion of files_include.php.
Basically, I need a general idea of were to look at and the things I should have in mind when rewriting root/test.php to root/search/A-B-C-D.html
Any suggestions?
Your browser is clueless about 'pretty' and 'ugly' urls. It just requests a folder or a file. If you request http://example.com/search/A-B-C-D.html, to the browser you are requesting a page A-B-C-D.html in the /search/ folder. If you have any relative urls on that page, it will request them relative to that /search/ folder. The browser has no clue, and should have no clue, what the internal representation of a request looks like. Heck, at your end of the line it might even be translated to instructions for a colony of hamsters, which will then send correct data through. The browser doesn't need to know how hamsters behave ;-)
The first problem is easily resolved by making your urls absolute. I wouldn't recommend making them relative to the pretty url. An alternate solutions would be to add the <base> tag to the <head> tag of your page. The href property of this tag will be used as a base for any relative links on your page. See mdn for more information. You would then do:
<head>
<base href="/">
</head>
As for your second problem, the include itself is not the problem. include(..) will first try to find the file in the include_path, and otherwise in the script's directory and the working directory. This doesn't change if you create pretty urls. Apache, and php, still know where the actual file is located you are executing. If an include statement fails to load a file it will generate an error too, which is another way you can tell if the include itself is the problem. See the documentation.
But the question is why is the relative path changing? Is .htaccess making the browser think we are in search'/ folder? The answer to this question will help me to identify the main issue, which is Problem2.
It's changing because the browser is loading /search/something-something-sometrhing-something.html instead of /test.php. The first URL has a relative URI base as: /search/ and the second URL has a base of /.
For the second problem, you could try externally redirecting, but not sure if that'll help the sitemap itself, it depends on the generator. Try adding this rule:
RewriteCond %{THE_REQUEST} \ /+test\.php\?cs1=([^&]*)&cs2=([^&]*)&cs3=([^&]*)&cs4=([^&\ ]*)
RewriteRule ^ /search/%1-%2-%3-%4.html [L,R]

Klein system. CSS isn't working

I'm testing Klein routing system, https://github.com/chriso/klein.php
it's awesome but i can't get my css ant images to run
This is my directory structure:
index.php
.htaccess
vendor
views
includes
assets
css
images
And here's one of my includes line of code where i try to access my assets/css:
<link href="../assets/css/main.css" rel="stylesheet">
I tried everything. Every possible hint would be great, thanks.
EDIT:
now i think it can be problem in my htaccess. how to make that url won't rewrite if it's css or img?
my .htaccess :
RewriteEngine on
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
Your link is most likely wrong (unless the code you pulled it from is in the include or assets folder). the ".." literally means "up one level". So if you code is in the .htaccess folder (which it usually is I believe) or any folder on that level the correct link would be:
href='../views/assets/css/main.css'
If your code is in css or images (not sure why it would be) the correct link would be:
href='../css/main.css'
Good lcuk!

Categories