I have starting creating a simple module system to help me build and visualise how all the pages of my application are going to be organised behind the scenes.
I'm starting to feel this isn't a very good way of achieving this as I will end up with a huge .htaccess file to rewrite all urls for each module.
RewriteEngine On
RewriteRule ^login$ index.php?page=login
Each module should have it's own controller, so that it is seperate from other module code, and any page inside any module should be able to access configuration array values from within the root index.php file.
<?php
if(isset($_GET['page'])) {
switch($_GET['page']) {
case 'login':
$page = 'modules/default/login.php';
break;
}
} else {
$page = 'modules/default/homepage.php';
}
include($page);
?>
I'm having issues as to what will happen when I add an account module that contains other pages. e.g. /account or /account/profile
<?php
if(isset($_GET['page'])) {
switch($_GET['page']) {
case 'settings':
$page = 'modules/account/settings/index.php';
break;
}
} else {
$page = 'modules/account/index.php';
}
include($page);
?>
Is there a better way this can be done?
Hopefully you can see what I'm trying to do.
Better to have your rule to catch all URIs that are not files or directories like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
Then use $_GET['page'] parameter to get all the URI values in your PHP code.
Related
favorite
I am building a website and I would like to customize the users' profile so their profile url shares their name. For example, the website domain would be www.example.com and the users' url would be www.example.com/username.
at present when i do a test sign it looks like test/admin/index.php
i want to rewrite it as test/admin/testuser
I am assuming this is a convention because I see this all around the web. Is this done by giving each user their own directory and how painstaking would that be?
Usually this is done by routing. You redirect all requests to the index.php file with .htaccess
Your setup could look like this:
.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?param=$1 [QSA,L]
index.php
<?php
if(empty($_GET['param'])) {
// code for frontpage
} else {
$username = $_GET['param'];
}
?>
In your htaccess you can try this:
RewriteRule ^/([a-zA-Z0-9_-]+)$ index.php?user=$1 [QSA,L]
And in your php:
if(isset($_GET['user'])) {
// code for frontpage
} else {
$username = $_GET['user'];
}
I hope this help you
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
My Code
RewriteRule ^walls/([0-9a-zA-Z-_]+) display.php?art_nm=$1 [NC,L]
What i get is:
website.com/walls/i_love_coding
what i need is:
website.com/i_love_coding
You probably should use the front controller pattern. Basically, you give every request to your front controller which decide what to do.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
</IfModule>
Then, use your "front controller" to forward request to correct script according to $_SERVER['REQUEST_URI'].
index.php
<?php
$name = trim($_SERVER['REQUEST_URI'], '/');
$page = find_page_by_name($name);
if ($page) {
require $page;
} else {
require __DIR__.'/errors/404.php';
}
Be carrefully in the find_page_by_name function to not return the path of an unauthorized file; you should ensure your path is within a specific directory (see realpath and strpos).
For example, an attacker could try to get http://example.com/../../etc/passwd !!!
For more complex routing, you should probably try a micro-framework like Silex or Slim.
i have a problem, my site give me this URL: mysite.com/?nav=1&action=inbox and i want to make it friendly, something like this: mysite.com/inbox
How can help me?
Hi, thanks everyone who help me, now i have: mysite.com/1/inbox.html and i want: mysite.com/inbox
Personally I use a generator for .htaccess rewriting.
Create a file called .htaccess and put it in your root folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
This basically says "if the URL is not a file or a directory, go to index.php".
Then your index.php should look like this:
<?php
$q = explode("/", $_SERVER['REQUEST_URI']);
if($q[1]!=""){
switch($q[1]){
case "articles":
include("articles.php");
break;
default:
include("error-404.php");
}
}
else{
include("home.php");
}
?>
Add a new case for each redirect you need. And use $q[2] for the second /.../ and so on.
I am designing my first blog website and would like to know how to implement SEO friendly url encoding.
At the moment I have this kind of URL:
http://mywebsite.com/article.php?id=2
But what I really want is this kind:
http://www.mywebsite.com/2013/11/09/this-is-my-article-title/
Could someone please point me in the direction a good tutorial for accomplishing this or perhaps explain how I can do it. I have search both here and google haven't yet found anything remotely helpful.
You can use something like this (put it in a file called .htaccess in the root of your website):
# Enables rewrite
RewriteEngine On
# Only continue if it's not a directory, file or link
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
# rewrite to file
RewriteRule ^(.*)$ index.php?page=$1
Then for http://yourwebsite.com/homepage you will get a request at index.php?page=homepage so that in PHP you can read out the page with:
if (isset($_GET['page'])) {
$page = $_GET['page'];
} else {
$page = 'defaultpage';
}
if ($page == 'homepage') {
// display homepage
} // etc.
For more information about this (so-called mod-rewrite or URL-rewriting), look up the Apache documentation of this.