htaccess with redirect page in php - .htaccess

I write the following code on htaccess
Options -Indexes
RewriteEngine on
RewriteBase /localhost_site_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ ?page=$1 [L]
# 404 redirect page start
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]
#404 redirect page end
I am displaying pages based on page concept so I will check the page before to displaying that details on page.
This is my index file
<?php
session_start();
ob_start();
?>
<!DOCTYPE html>
<body>
<?php
//echo "Page name is ".$page;
if($page=="template" || $page=="user")
{
include("modal.php");
include("topnav.php");
include("sidebarnav.php");
include("content.php");
include("footer.php");
}
else
{
echo "<h1 style='background:#000;color:#fff;'>404 Error</h1>";
}
?>
</body>
</html>
Now my problem is when i display the pages with one argument based on my page name its shows correctly
But when i try to pass argument with that it always went to 404 page
I couldn't find out that why its happening
So help me to pass two argument with htaccess used on page concept
working url: http://localhost/sitename/pagename/
not working url: http://localhost/sitename/pagename/argument

Related

Redirecting to another page - htaccess

I'd like to redirect the user to a page named test.php when he types the url of my site (example www.domain.com).
Pratically by typing www.domain.com the browser should show www.domain.com/test.php
At index.html or index.php, you could add meta refresh tag like this :
<meta http-equiv="refresh" content="5;www.domain.com/test.php" />
If you use index.php, the syntax would be like this:
echo '<meta http-equiv="refresh" content="5;www.domain.com/test.php" />';
Add http:// at the url if You want to redirect to another domain.
In top of your .htaccess file try this rules (except files and directories rewrite anything www/non www to example.com/test.php)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule (.*) http://example.com/test.php [L]
</IfModule>

how do i redirect my URL in with folder htaccess

my url
www.mysite.com/content
How can i redirect my site if type wrong URL like this
www.mysite/contentfasfsa(any letter)
That would redirect to original site www.mysite.com/content/ how could i do that.
You will have to edit your .htaccess file. There are two ways for doing this :
1. ErrorDocument 404 /content (Your home page)
This will redirect all the error 404 to your homepage.
2. RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ www.mysite.com/content [L]
This rewrite rule will redirect broken links to your homepage.
Try them out.

Login and Index page, same url, htaccess

I have a login like this:
www.mysite.com/login
and an index page like this (acting as a home page)
www.mysite.com
How can I make the login and the index have the same URL with htaccess ? -> www.mysite.com
UPDATE NOTE:
If my user is logged in go to index.php page = www.mysite.com url
If my user is not logged in go to login.php page = www.mysite.com url
Very simple should be like this
DirectoryIndex login/ index.php
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login$ index.php [L]
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?section=([a-z0-9]+)&title=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?section=$1&title=$2 [L,NC]
this shoud be added to your .htaccess
and your project will become one page to direct the links which is index.php
you see this in the htaccess code (index.php?section=$1&title=$2)
it will take the links and send it as parameters
for example www.yoursite.com/login means index.php?section=login
and from you php file
type this
<?php
if ($GET['section'] == "login"){
// include your page
echo 'my login page';
}else{
echo 'error 404';
}
hope you got it
i got your question my friend
you can only make it by the sessions or cookies
so if the user logged in you should have create a sessions by php
for example
<?php
// Start the session in every page you will use it
session_start();
// when log-in is happen you give SESSION this
$_SESSION["user_logged"] = "ture";
//then to check if hes logged or not
if ($_SESSION["user_logged"] == "ture";){
echo'put your member page here';
}else{
echo'put your loging stuff here';
}
?>

Htaccess file - 404 page not found on some folders

I am using codeigniter and I keep having these 404 errors in my log file.
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js
ERROR - 2016-04-30 16:41:18 --> 404 Page Not Found: Assets/js
So every page load causes one entry of the above error in the log file. My htaccess file looks like below. What's going wrong here?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|assets|myadmin|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I can confirm assets/js are valid folders.
Are you able to get to your assets folder by going through the web path? e.g. http://localhost/assets (try to retrieve a file if you dont have indexing enabled in your webserver e.g. http://localhost/assets/css/style.css), does it load?
If this is loading you are possibly calling your assets without using a base path which means a view is trying to call directly from within its own folder.
Using site_url() is a really good step (url helper)... I urge you to make your life easier in calling assets by making a simple helper and adding it to auto load containing (something like) this:
if (!function_exists('assets'))
{
/**
* Base asset directory call
*/
function assets($var)
{
$CI =& get_instance();
$CI->load->helper('url');
return site_url('assets') . '/' . $var;
}
}
You would then call it in your view:
<!-- load CSS -->
<link href="<?php echo assets("css/bootstrap.min.css") ?>" rel="stylesheet">
It would then show this once rendered:
<link href="http://localhost/assets/css/bootstrap.min.css" rel="stylesheet">
This is just a really basic example though, if you have heaps of assets to call in a header or footer, I would suggest creating a helper to load based on a config file, you can also specify what is required on certain page loads that way as well so its dynamically loaded with a foreach.
Also do be careful calling Assets (capital letter) vs assets (no capital letter) is very different, many webservers are case-sensitive...
I run the following for my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Redirect 404 URL to search page with htaccess and pass that 404 url as keywords to search page

I want to redirect 404 page to search page.
For example,
example.com/url - if this url is not found then redirect the url in the format:
example.com/index.php?page=search/web&search=[url]&type=Web&fl=0
It has to be done with htaccess. I was not able to achieve this with htaccess.
Try the following rule in root/htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?page=search/web&search=$1&type=Web&fl=0 [NC,L,R=301]

Resources