URL redirecting in localhost - php

I am using a simple htaccess file to rewrite urls of my project. I rewrite the whole url in a get variable, then use php to parse it.
My problem is when I try to send redirect headers, I can't seem to get it right.
For example, say I am viewing the page
localhost/myProject/home
With the header
header('Location: login');
it redirects to
localhost/myProject/home/login
When I try
header('Location: /login');
it takes me to
localhost/login
which does not exist and I get a 404 error.
So far the only way I have found to work is when I do
header('Location: http://localhost/myProject/login');
but I don't think that is a good option because when my site goes live I will have to change every single header.
Is there a better way to do it?
My .htacces file is:
RewriteEngine On
RewriteBase /myProject/
#Make sure it's not an actual file
RewriteCond %{REQUEST_FILENAME} !-f
#Make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#Rewrite the request to index.php
RewriteRule ^(.*)$ index.php?get=$1 [L]

you can do it like this : Use base url variable for that.Define that variable global or in config file.You have to only change that variable one time:
$base_url = "http://google.com"; or define('base_url', 'http://google.com');

Related

How to redirect sub-directory to root directory with parameter as sub-directory using .htaccess

Want to Redirect to index.php
When user try to come at site using any URL like:
1)www.domain.com/xyz
2)www.domain.com/abc
3)www.domain.com/apple
4)www.domain.com/123
5)www.domain.com/hello
Whatever random text at the end of the URL.
I want .htaccess code which redirects those URL to
www.domain.com/index.php
index.php contain code to get data using GET method so the last string of URL needs to get in index.php file.
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/index.php?parameter=$1
I tried this code but it goes in infinite redirect.
I want to stop loop while parameter found.
You should be redirecting to a relative address (assuming that index.php is on the same domain). Also, you need to exclude existing files:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

Rewrite site with first slash to index

I'm sorry about the title but I can't find a good one to explain what I try to do.
I have my website : http://domaine.com .
The index.php file in it redirect to http://domaine.com/views/index.php .
Now I want to Rewrite Rule so that any url like :
http://domaine.com/something display the site http://domaine.com so that :
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
etc.
EDIT : I'm working on localhost so it's supposed to:
localhost/formation/php => localhost/formation/php/views/index.php
localhost/formation/java => localhost/formation/java/views/index.php
localhost/formation/ruby => localhost/formation/ruby/views/index.php
EDIT : The content of my htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ views/index.php [L,QSA]
When I access to localhost/formation/ruby/ for exemple it's working but after the loggin it look like it's always redirecting from the home page to login page to home page etc.
EDIT : I create a repertory called "ruby" and create in it a htaccess file with that content.
RewriteEngine On
RewriteRule ^(.*)$ ../$1 [L]
And it does exactly what I want. So I want to find a way to do that without having to create the repertory "ruby".
http://domaine.com/php redirect http://domaine.com/php/views/index.php
http://domaine.com/java redirect http://domaine.com/java/views/index.php
http://domaine.com/ruby redirect http://domaine.com/ruby/views/index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(php|java|ruby)/$
RewriteRule ^.*$ /%1/views/index.php
I suppose by "redirect" you mean "serve as". In case if you want a 301 redirect, use the appropriate flags for RewriteRule, e.g. [R=301,L].
Wrapper
In case if you are trying to implement a wrapper script which is supposed to handle different request URIs, you should process the requests with a single script and pass the URI path parts as query string parameters. For instance:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(formation|another_type)/(php|java|ruby)/$
RewriteRule ^.*$ /wrapper.php?type=%1&lang=%2
With this configuration, you can handle requests like http://your-site.com/formation/ruby/ with a single script. You can access the query string parameters using the $_GET superglobal variable, e.g. echo $_GET['lang'];.
Ultimately, you can process all requests with a single wrapper:
RewriteRule ^.*$ %{DOCUMENT_ROOT}/wrapper.php
Within the wrapper you will have to examine the contents of the $_SERVER superglobal. The REQUEST_URI and REQUEST_URI values are particularly useful.

Redirect the URL to another, in codeigniter

I have a webpage which was built with core php, now its been updated to codeigniter mvc. Problem which i am facing is, users are trying to enter the old URL path
example: www.website_name/old_path
now since the mvc layout, it wont work and will get 404 error. i want to redirect the clients who enters www.website_name/old_path for the new one
www.website_name/index.php/new_controller/new_view.
Anyone please help me to resolve this issue.
Thank You!
first of all remove index.php config file.
Open config.php and do following replaces
$config['index_page'] = "index.php"
to
$config['index_page'] = ""
Then create HTACCESS file in application/ folder
having this code....
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Now index.php is removed from your URl....
STEP 2) open application/config/routes.php file
Now here define your desired web URL
$route['mycontroller/(:any)'] = "mycontroller/user_detail/$1";
So,Your URL Will be ..
www.website_name/new_controller(or xyz)/
Here you can also change the controller name differently using routes So no one can get the exact name of controller/method name....To Do such customization refer this URL
http://www.codeigniter.com/user_guide/general/urls.html
As Sumit mentioned, you should use URL Routing for this.
In the routes file you can create a list of all the redirects that you want like so:
$route['old_path'] = "new_controller/new_view";
You can use 301 redirect for this type of issue.
Put this code in .htaccess
RewriteRule ^www.website_name/old_path www.website_name/index.php/new_controller/new_view [L,R=301]
or use 301 redirect of codigniter http://www.codeigniter.com/user_guide/helpers/url_helper.html
// with 301 redirect
redirect('/article/13', 'location', 301);

is there an easy way to avoid creating all the folders

Ok so I have this site.... with the url
http://posnation.com/
and there are alot of pages that i need to save the url structure for....like this
http://posnation.com/restaurant_pos
http://posnation.com/quickservice_pos
http://dev.posnation.com/retail_pos
ext....
The problem that i have now is that i want to save the same url for all these pages and I am looking for the best approach. The way its working now is its done with a code in miva and we are getting off miva.... I know I can create a folder named restaurant_pos or whatever the url is and create an index.php in there.This approach will work but the problem is I need to do this for 600 different pages and I dont feel like creating 600 folders in the best approach.
any ideas
You should use .htaccess to route all the requests to a single file, say index.php and do the serving from there based on the requested URL.
The following .htaccess file on the server root will route all the requests to your index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>
Now you should parse the $_REQUEST['route'] to identify which file you should serve. Here is an example that will serve a page based on the last element of the URL (ex: pos):
<?php
$parts = explode($_REQUEST['route']);
if ($parts[count($parts) - 1] == 'pos') {
include "pages/pos.php";
}
Definitely you'll need to write your own logic, the above is just an example.
Hope this helps.
Usually the easiest way to do this is to create an .htaccess file that redirects all requests to /index.php. In /.index.php you analyze the URL using probably $_SERVER['REQUEST_URI'] and include the appropriate content.
Heres a sample htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
In your /.index.php do something like ... (this is just a VERY simple example)
require 'pages/' . $_SERVER['REQUEST_URI'] . '.php';
Now if someone goes to http://posnation.com/restaurant_pos pages/restaurant_pos.php will be included.
pages/restaurant_pos.php could include the header and footer too.
<?php require( HEADER_FILE ) ?>
restaurant_pos content
<?php require( FOOTER_FILE ) ?>

PHP & MySQL url rewriting question

Can I rewrite a url by using the value 6 from ?cat for example, and checking my MySQL database for the value 6 category name html using PHP & MySQL if so how?
Current url.
http://localhost/index.php?cat=6&sub1=8&sub2=24&sub3=81
New search friendly URL displayed in browser.
http://localhost/html/basics/forms/select-tag/
Check out mod_rewrite.
You need to open up the .htaccess file(if it does not exist, create it) and include the following lines. Note that RewriteEngine On should be only written once.
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^html/basics/forms/select-tag/([0-9]+)/?$ index.php?cat=$1 [NC,L]
Yes. Basically you will want to pass the entire url through a router script using mod_rewrite. The .htaccess file should have something like this:
RewriteEngine On
RewriteBase /
#if it's an existing directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
#...or an existing file
RewriteCond %{REQUEST_FILENAME} -f
#serve it up
RewriteRule ^(.+) - [PT,L]
#else, direct to index.php
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Then, in index.php you can have something like this:
$request = explode('/', $_GET['url']);
Which would have all your clean url segments in the $request array. Once you have those values, you can connect to the database, find what the url represents, and output the page. If it's not found, you can send a 404 header with a "Page not found" message.
header('HTTP/1.0 404 Not Found');
echo '<p>Page not found</p>';
So that's the basic clean url technique. If the url is the earlier, messy one, you would just add some logic to check for that, and redirect to the correct clean url:
if(isset($_GET['cat'])){
//put some logic here
header("Location: http://localhost/".$the_clean_url);
}
This basic technique should solve your problem with some tinkering.

Categories