http://localhost/customers/website/jobs/28
When for an example the URL above is visited and my code is used, it goes to: http://localhost/customers/website/jobs/startpage
if(isset($_COOKIE["startpage"])) {
}else{
header("Location: startpage");
}
But if I do header("Location: /startpage"); it becomes http://localhost/startpage
My Htacess is:
RewriteEngine On
RewriteBase /customers/website/
RewriteRule ^startpage/?$ startpage.php [NC,L]
How can I make it go to http://localhost/customers/website/startpage?
Don't use PHP for this. You can do this in htaccess itself:
RewriteEngine On
RewriteBase /customers/website/
RewriteCond %{HTTP_COOKIE} !startpage
RewriteRule !^startpage/?$ startpage [L,NC,CO=startpage:1:%{HTTP_HOST},R=302]
RewriteCond %{HTTP_COOKIE} startpage
RewriteRule ^startpage/?$ startpage.php [L]
and remove your header function call from PHP code.
If you will want to do this in PHP itself then use:
if(isset($_COOKIE["startpage"])) {
//
} else {
header("Location: " . $_SERVER["BASE"] . "startpage");
}
and keep same rewrite rule shown in question.
Related
So I'm working on a web application much similar to Guild Hosting & Clan Hosting sites. But I'm having a problem with the HTACCESS and PHP.
Here is my .HTACCESS file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule .* /index.php?site=%1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/?site=$1 [L,R=301]
Here is my INDEX.PHP file:
<?php
$site = explode(".", $_SERVER["HTTP_HOST"]);
if(sizeof($site === 3) && $site[0] == "www") {
echo "Welcome to MySite!";
} else {
if(isset($_GET["site"])) {
echo "Welcome to " . $_GET["site"] . "!";
} else {
header("Location: http://www.example.com/");
}
}
?>
What I want the code to do is if they go to the main site it just echos that basic message but I keep getting
http://www.example.com/?site=
at the end of my domain when I don't want anything there, I also want to be able to make it so the site redirects to a subdomain of the specified site e.g.
http://www.example.com/?site=test => http://test.example.com or http://www.test.example.com
I then want the htaccess file to get the information from
http://www.example.com/?site=test/
like the htaccess currently does, anyone know how I could achieve all of this in HTACESS and PHP?
So I was bored of waiting for an answer and so I messed around with my .HTACESS file and it works!
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{REQUEST_URI} !index\.php
RewriteCond %{HTTP_HOST} ^(.+?)\.example.com$
RewriteRule .* /index.php?site=%1 [L]
and the index.php file manages redirecting to the WWW site:
<?php
$site = explode(".", $_SERVER["HTTP_HOST"]);
if(sizeof($site === 3) && $site[0] == "www") {
echo "Welcome to Main Site!";
} else {
if(isset($_GET["site"])) {
echo "Welcome to " . $_GET["site"] . "!";
} else {
header("Location: http://www.example.com/");
}
}
?>
I just started to learn htaccess and i'd like to rewrite my current urls from this:
http://www.url.com/?location=script
To:
http://www.url.com/script
So far i've managed to do this but now i want to have a directory with more controllers so i can have something like this:
http://www.url.com/script/method
Structure: Script directory --> method.php
Currently my directory structure for includes its like this:
assets-->client(directory):
login.php
logout.php
register.php
something.php
And i'd like to access these using a url like:
url.com/client/login
url.com/client/logout
url.com/client/register
url.com/client/something
My .htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?location=$1 [L]
</ifModule>
PHP based inclusion code:
####################################################################
# PARSE THE CURRENT PAGE #
####################################################################
$includeDir =".".DIRECTORY_SEPARATOR."assets/controllers".DIRECTORY_SEPARATOR;
$includeDefault = $includeDir."home.php";
if(isset($_GET['ajaxpage']) && !empty($_GET['ajaxpage'])){
$_GET['ajaxpage'] = str_replace("\0", '', $_GET['ajaxpage']);
$includeFile = basename(realpath($includeDir.$_GET['ajaxpage'].".php"));
$includePath = $includeDir.$includeFile;
if(!empty($includeFile) && file_exists($includePath)) {
include($includePath);
}
else{
include($includeDefault);
}
exit();
}
if(isset($_GET['location']) && !empty($_GET['location']))
{
$_GET['location'] = str_replace("\0", '', $_GET['location']);
$includeFile = basename(realpath($includeDir.$_GET['location'].".php"));
$includePath = $includeDir.$includeFile;
if(!empty($includeFile) && file_exists($includePath))
{
include($includePath);
}
else
{
include($includeDefault);
}
}
else
{
include($includeDefault);
}
All my controllers are in assets/controllers/ucp/login.php for example.
How about:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(/client/[a-zA-Z0-9_\-]+)$
RewriteRule ^[a-z]+ index.php?location=$2 [L]
</ifModule>
It does include a leading slash and doesn't have the PHP extension. But this can be altered in PHP to suit your needs.
Be wary though, your code gives access to all your PHP files. You might want to check $_GET['location'] against an array of allowed locations. Consider the following URL as example of how this could go wrong
http://example.com/index.php?location=../drop_database.php
I'm trying to get action value from variables in this url.
Real Url
index.php?action=ok&id=45&name=LG-Optimus
Friendly Url
home/ok/1/LG-P88
This above friendly url show when redirect an page like this
header('Location: /home/ok/' . $id . '/' . $name);
The page script that contains the code to get the variables
$ok = isset($_GET['ok']) ? $_GET['ok'] : "";
if($ok=='ok'){
echo "<div class='popup'>";
echo "<strong>{$card-name}</strong> added!";
echo "";
echo "</div>";
}
But i get 'Undefined index ok'.
The .htaccess file contains this code
# Turn Rewrite Engine On
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# Rewrite for index.php
RewriteRule ^home index.php [NC,L]
# Rewrite for card-name
RewriteRule ^home/([0-9]+)/([0-9a-zA-Z_-]+)$ index.php?action=$1&id=$2&name=$3 [QSA,L]
Thanks for any help.
You are setting the GET-variable action to "ok", but your code presumes the variable is NAMED "ok".
To check it in your code you should use:
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='ok'){
Another thing, your regular expression lacks the "action" argument, try this:
^home/(.*)/([0-9]+)/([0-9a-zA-Z_-]+)$
You have some errors in this code. use this code
RewriteRule ^home/([a-z]+)/([0-9]+)/([0-9a-z_-]+)$ index.php?ok=$1&id=$2&name=$3 [QSA,L,NC]
Also for security reasons you must avoid from direct use of $_GET . (see)
So I am pretty new to url rewritting and.htaccess in general.
I would like to rewrite the url below
/rewrite/profile/karis/2
as
/rewrite/profile.php?name=karis&id=2
so the code I have for my .htaccess is
RewriteEngine on
RewriteRule ^profile profile.php
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ profile.php?name=$1&id=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ profile.php?name=$1&id=$2
So this works perfectly when I change everything to work for index.php but the same code does not work on profile.php.
The simple php code is
<?php
if(isset($_GET['name']) && isset($_GET['id']))
{
echo "Hi ".$_GET['name'].", your id is ".$_GET['id'];
}
?>
Any Ideas why?
Your rules can all be combined into one and first one is misplaced also. Try this in your /rewrite/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /rewrite/
RewriteRule ^([\w-]+)/([0-9]+)/?$ profile.php?name=$1&id=$2 [L,QSA]
RewriteRule ^profile/?$ profile.php [L]
I have many articles in foo.com/articles/
like this:
foo.com/articles/banana.php
foo.com/articles/strawberry.php
and want redirect SEO to subdirectory like this:
banana.foo.com
strawberry.foo.com
EDIT
when I type (banana.foo.com) show me (foo.com/articles/banana.php).
just show and not realy redirect
I can not create many subdomain by Cpanel, therefore (banana.foo.com) do not create by Cpanel
put this in your .htaccess file ( don't forget to replace "yourdomain" with your domain name)
Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*).yourdomain.com$
RewriteRule (.*) yourdomain.com/articles/%1.php [L]
Now when you request
banana.foo.com
The result will come from
foo.com/articles/banana.php
and so on
You might do something like:
if (preg_match('#.*/articles/([a-z0-9\-_]+)\.php#i', $_SERVER['PHP_SELF'], $match)) {
header('Location: http://' . $match[1] . '.foo.com');
exit;
}
Remember to paste this code before any other output or you'll get an error.
Or via .htaccess with mod_rewrite enabled and RewriteEngine ON:
RewriteRule ^.*/articles/([a-z0-9\-_]+)\.php$ http://$1.foo.com [R,L]
EDIT
The reverse would be something like:
if (preg_match('#^http://([a-z0-9\-_]+)\.foo\.com.*$#i', $_SERVER['HTTP_HOST'], $match)) {
header('Location: http://foo.com/articles/' . $match[1] . '.php');
exit;
}
And with .htaccess:
RewriteCond %{HTTP_HOST} ^([a-z0-9\-_]+).foo.com$
RewriteRule .* http://foo.com/articles/%1.php [R,L]
In .htaccess you can do this with the following:
RewriteRule ^foo.com/articles/([a-z0-9-]+).php $1.foo.com [PT,L]
You'll need mod rewrite installed, and you'll need a wild card subdomain set up in your DNS server. http://en.wikipedia.org/wiki/Wildcard_DNS_record