.htaccess with dynamic php - subfolder problems - php

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...

Related

htaccess redirect to subfolder, if URL contains string

I feel really sorry, cause I feel like this is a simple question and it was answered few times, but I can't solve my problem ;(
I have a folder structure like this:
restaurant (folder)
.htaccess
index.php
anotherfile.php
few_more_files.php
htaccess file in root directory contains this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(test\.pdf|\.png)$ [NC]
RewriteRule ^(.+)$ index.php [QSA,L]
I need a way to implement logic:
if URL contains mydomain.com/restaurant/any_other_strings_here, I need to redirect to /restaurant/index.php file
What should I do? Thanks in advance!
You have to handle domain and url separatetly:
# Only redirect if domain = dmydomain.com
RewriteCond %{HTTP_HOST} ^mydomain\.com$
# Redirect only if url contains restaurant/any_other_strings_here
# L = last = End htaccess evaluation after this rule
# R=301 = Redirect with status 301
RewriteRule restaurant/any_other_strings_here$ /restaurant/index.php [L,R=301]
Probably "any_other_string" refers to something wildcardish, so this might be better for you:
RewriteRule restaurant/(.*)$ /restaurant/index.php [L,R=301]

Rewrite profile.php?user=username to profile/username

I've created a very basic php templating system that I'm using to display a page with this URL: /basedir/index.php?page=home
The home content is served by the script home.php.
With a rewrite rule in .htaccess, I want my url to look like this: /basedir/home.
The problem that I'm having is that I've got stuck when it comes to rewrite one more GET variable that I'm using to display a user's profile. For now the profile page url looks like this: /basedir/profile?user=username.
Now I want it to look like this: /basedir/profile/username.
So far, my .htaccess looks like this:
RewriteEngine On
RewriteBase /basedir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) ?page=$1 [L,QSA]
I have no idea if this is good or if i have to write a completely new .htaccess file.
Based on your code, and my limited understanding, I would guess something like this:
RewriteRule ^profile/([^?]*) ?page=profile&user=$1 [L,QSA]
.. insert it above your RewriteRule ^([^?]*) ?page=$1 [L,QSA].
As you can see at http://htaccess.mwl.be/ your url http://www.example.com/profile/username is converted into http://www.example.com/basedir/?page=profile/username
Now what you need to do is put index.php in basedir where explode the page and retrieve username.
$page = $_GET['page'];
$parts = explode('/', $page);
switch($parts[0]){
case 'profile':
if(isset($parts[1])){
$username = $parts[1];
return doWhatEverYouWantToDoWithUsername($username);
}
return handleJustProfilePage();
default:
return yourDefaultHandler();
}
This approach is called routing you can make way better with some external routing libraries.

htaccess file for dynamic web pages

my current url : http://example.com/blog-single.php?id=15
this is a dynamic blog page the "id" has to be change accordingly to blog adding
I need to change this url to http://example.com/blog-single/<coresponding-blog-name>
eg: http://example.com/blog-single/my-new-blog
how to Remove .php and id from the current URL?
First of you need to create a .htaccess file and add this code in it
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog-single.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog-single.php?id=$1
After that in your main file for example in index.php file you need to add the code
<?php
$key=$_GET['key'];
if($key=='my-new-blog')
{
include('my_new_blog.php'); // Home page
}
else if($key=='login')
{
include('login.php'); // Login page
}
else if($key=='terms')
{
include('terms.php'); // Terms page
}
else
{
include('users.php'); // Users Gateway
}
?>
try this according to your need.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/([a-zA-Z0-9_-]+)$ $1.php?id=$2 [NC,L]
here you can use your filename in manner http://example.com/blog-single/my-new-blog this will work for every page as per your requirement.
For example, if you access to this: http://example.com/blog/1234.html and real path is http://example.com/blog.php?id=1234 you can put this on your htaccess file:
RewriteEngine On
RewriteRule ^blog/([^/]*)\.html$ /blog.php?id=$1 [L]
This tool will help you: http://www.generateit.net/mod-rewrite/index.php

Url rewriting for all identical filenames

I would like to rewrite all my URL's like so:
http://example.com/folder/40/name/test.php
and
http://example.com/folder/40/test.php
to
http://example.com/test.php
Does anyone have a solution?
thanks
You can do that by using htaccess. It will redirect all urls like
http://example.com/folder/40/name/test.php
http://example.com/folder/40/test.php
to
http://example.com/test.php
In the htaccess file add below lines
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteRule ^(.*)/(.*)/(.*)/test.php$ test.php [L]
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteRule ^(.*)/(.*)/test.php$ test.php [L]
more information Htaccess Rewrites
You can easily do it by PHP but you have to include this code in both pages before opening HTML tag.
<?php
$useragent=$_SERVER['HTTP_USER_AGENT'];
header('Location: http://example.com/test.php'); ?>
Hope this will help you!

htaccess and php - redirects and pretty urls

I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.
Right now my htaccess looks kind of like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
You are able to link to users like this domain.com/username and that's nice.
Then I have different pages like
index.php?page=forum&id=1
index.php?page=someotherpage&id=1&anotherid=5
index.php?page=3rd
... and so on. I want them to look something like this:
domain.com/forum/23/title-of-the-thread
domain.com/page2/id1/id2
... and so on.
How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?
I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.
If all of the urls you are going to rewrite are going to the same end point, you could simply use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
in index.php:
<?php
$url = $_SERVER['REQUEST_URI'];
How you use the request uri is up to you, you could for example use a simple strpos check:
<?php
$url = $_SERVER['REQUEST_URI'];
$rules = array(
'/forum/' => 'forum',
'/foo/' => 'foo',
'/' => 'username'
);
foreach($rules as $pattern => $action) {
if (strpos($url, $pattern) === 0) {
// use action
$file = "app/$action.php";
require $file;
exit;
}
}
// error handling - 404 no route found
I was thinking about creating a file that checks the URL,
you actually have that file, it's index.php
if it matches any pages, and users and so on. Then it will redirect with a header location.
that's wrong. HTTP redirect won't make your URLs look "pretty"
you have to include appropriate file, not redirect to.
Just change your rule to more general one
RewriteRule ^(.*)$ index.php [L,QSA]
You basically have two options.
Route all URLs to a central dispatcher (FrontController) and have that PHP script anaylze the URL and include the correct scripts
Note every possible route (url rewrite) you have in the .htaccess
I've always worked with option 1, as this allows greatest flexibility with lowest mod_rewrite overhead. Option 2 may look something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L]
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L]
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L]
# …
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
you said
I was thinking about creating a file that checks the URL, if it
matches any pages, and users and so on. Then it will redirect with a
header location.
While "creating a file that checks the URL" sounds a lot like option 1, "redirect with a header location" is the worst you could do. That would result in
an extra HTTP roundtrip for the client, leading to slower page loads
the "pretty URL" won't stick, the browser will show the URL you've redirected to
losing link-juice (SEO)
This can be done entirely with htaccess or php
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2
read more about it here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

Categories