Do not get mod_rewrite working with $_GET['page'] - php

My current link is:
domain.com/folder/index.php?page=home
and i try to get it to this:
domain.com/folder/home
The PHP code that I use to get the page
<?php
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'home';
}
if (strpos($page, '/') !== false || !file_exists("pages/$page.php")) {
$page = 'error';
}
include ("pages/$page.php");
?>
I tryed a lot to get the right .htaccess code to rewrite my URL, from generators to forum posts but I can not get the right code.
I hope someone can get me in the right direction to get this working.

these .htaccess rules should help you get started
RewriteEngine On
RewriteBase /
#### this allows you to write your links in the form /folder/home ####
RewriteRule ^folder/([0-9]+)$ folder/index.php?page=$1 [NC,L]
#### to dynamically redirect to /folder/home ####
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?page=([^&\s]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]

Redirect ^(.*).php$ index.php?page=$1 [L,QSA]
utilize this in your htaccess

Related

htaccess hide file name from url

I need to hide the profile.php from appearing in the URL.
So here's what I have:
The URL I am accessing:
sampleuser.domain.com/profile.php
How I get the user 'sampleuser':
$url = "//".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
/*this will give me $url = '//sampleuser.domain.com/profile.php'; */
function get_user_from_url($url)
{
if (strpos($url , 'www.') !== false) {
preg_match('/\/\/www.(.*?)\.domain/', $url, $val);
}
else
{
preg_match('/\/\/(.*?)\./', $url, $val);
}
return $val[1];
}
$user = get_user_from_url($url);
echo $user;
I just need to hide the profile.php, also the thing to consider is if we hide the profile.php, it might conflict with the index.php.
But luckily we will only access the index.php if the URL is only domain.com (without user).
I'm low on htaccess knowledge, can anyone give me the direct answer, and I would be very appreciative if you can add some explanation, it'll give me a head start on learning htaccess.
So basically, what I was making is a dynamic subdomain. So I added a subdomain on my DNS:
*.domain.com
then added these on my htaccess
<IfModule mod_rewrite.c>
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^/?$ /agent/index.php?user=%2 [L]
</IfModule>

PHP htaccess wrong with URL's

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.

rewrite pretty urls with php and htaccess, mapping 404 for subpages

In .htaccess I have following rules for having page/subpage rewrite.
#Permalinks
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine On
#catch potential subpages first
RewriteRule ^([a-z\-]+)/([a-z\-]+)/?$ index.php?page=$1&subpage=$2 [L]
#
RewriteRule ^([a-z\-]+)$ index.php?page=$1 [L]
</IfModule>
In php I do this:
if (!$_GET) {
//
$page = "home";
$page_title = "General Site Title";
include ("template/index.php");
//
} else if ($_GET['page'] == "words") {
//
$page = "words";
$page_title = "Words Page";
include ("template/words.php");
//
} else if ($_GET['page'] == "about") {
//
$page = "about";
$page_title = "About Page";
include ("template/about.php");
//
} else {
//
header("HTTP/1.0 404 Not Found");
//
include ("template/404.php");
//
exit();
//
}
This works great, for showing 404.php when page doesn't exist.
But what if I want to have 404.php also when page/subpage doesn't exist?
For example I'm planning to have /members page. If someone goes to non existing /members/user1, I need to check DB for existing user, if doesn't exist I should show 404.php but is this correct logic to use in this situation?
p.s. how do I add rewrite /about/ to show same page? currently /about returns 200, but /about/ returns apache page not found.
First of correction in rules to make trailing slash optional in both rules:
RewriteEngine On
RewriteRule ^([a-z-]+)/([a-z-]+)/?$ index.php?page=$1&subpage=$2 [L,NC,QSA]
RewriteRule ^([a-z-]+)/?$ index.php?page=$1 [L,NC,QSA]
Now about checking /members/user1 yes you can check DB for existence of member record and if not found you can return 404 status/page.

htaccess URL rewrite with $_get function usage

I have been trying to do a htaccess URL rewrite (R=302) for a website which uses a $_get function to pull the content pages by use of strings. For instance, I am trying to change the following:
http://www.site.com/index.php?page=about
Into this:
http://www.site.com/about/
However, I have managed to get it set up to the point where it will redirect the header and footer data within the index.php file if I use query the second URL, but it will not grab the CSS or the $_get information for the content. Here are the htaccess entries and the $_get information:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/$ /index.php?page=$1 [NC,QSA,L]
RewriteRule ^(.*?)/$ $1.html [NC,L,QSA,R=302]
<?php
$folder = '';
$ext = 'html';
if (!$_GET['page'] || strpos($_GET['page'], '/') === true)
$path = 'home.' . $ext;
else $path = $_GET['page'] . '.' . $ext;
if (!file_exists($path)) {
$messages = array(
'404: Page does not exist; Please check the URL you entered.',
'404: Error 404, please check the URL of the page.'
);
print $messages[ rand(0, (count($messages) - 1)) ];
}
elseif (!#include($path)) print '300: couldn\'t get the page';
?>
Any help would be appreciated. I have been trying to modify the php code unsuccessfully thinking that is the issue.
First off, your two rewrite rules have the same condition, so only the first one will ever be met. I think you probably only want to rewrite if the requested uri doesn't exist on the server.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php?x=$1 [L]

.htaccess with dynamic php - subfolder problems

Necessary Knowledge
My .htaccess file redirects like this:
domain.com/about to domain.com/index.php?page=about
RewriteRule ^([^/.]+)/?$ index.php?page=$1 [L]
The "page" variable is used in a php include:
<?php include_once($_SERVER['DOCUMENT_ROOT']."/contents/".$page.".html"); ?>
The "contents" folder simply contains .html files that are included as the content
Okay here's the problem:
I have a "subfolder" in the "contents" folder with additional .html files that I need to access
Now I'm trying to redirect like this:
domain.com/subfolder/about to domain.com/index.php?page=subfolder/about
This works:
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1
But now I can't access the subfolder from: domain.com/subfolder/ because there is a 'page' variable
<?php $page = $_GET['page']; if(!$page) { $page = 'index'; } ?>
Any thoughts, ideas, or help would be greatly appreciated.
Thanks in advance
With this, you shouldn't have to define any directory names - it rules them all out.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1
You may need to test the trailing slash though, it may work on /subfolder/ but not /subfolder
You could exclude specific folder via
rewritecond %{REQUEST_URI}!^/folder1/
rewritecond %{REQUEST_URI}!^/folder2/
RewriteRule ^([^/.]+/[^/.]+)/?$ index.php?page=$1
Edit: Link to the manual
Just do another check:
if (preg_match('/^[^\\/.]+$/', $page)) {
$page .= '/index';
}
If $page is just subfolder, /index would be appended to have subfolder/index.
rewriteRule ^foldername/.*$ - [PT]
rewriteRule ^foldername/.*$ - [PT]
before any other rewrite rule...

Categories