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.
Related
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.
I looked at the suggested answer(possible duplicate as stakoverflow says). It provided help but didn't solve the problem as it won't help create clean URL as per my needs. It has controller as part of the URL which I don't want to see as mentioned below.
I am developing a tutorial site in Codeigniter, and looking for a way to generate SEO friendly URL like
www.crdlabs.com/how-to-use-multidimensional-arrays
instead of
www.crdlabs.com/home/index/how-to-use-multidimensional-arrays.
Can anyone share his ideas? Thank you
First you need to hide the index.php from URL so create the .htaccess file on the root folder and add this code
.htaccess
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Then open the routes.php file in the application/config/routes.php location
and add this
$route['how-to-use-multidimensional-arrays'] = 'home/how-to-use-multidimensional-arrays';
This will work as URL as SEO friendly
Add this to ur root .htaccess file :
RewriteRule ^(.*)$ index.php?title=$1 [NC,L,QSA]
Then in ur index.php get the title $title = $_GET['title'] and do what ever you like with it like do a query in ur database and return data based on the title !
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
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...