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
Related
I'm creating a blog, and would like to use the title of each entry as the URL.
The blog is pulling from a database and has code in a single php file to display the blog entry.
I would like any url like, domain.com/blog/this-is-the-title.html to redirect to
domain.com/blog/index.php
BUT, keep the URL in the browser bar as the original url.
EDIT...
domain.com/blog/anything-that-is-here.html
should redirect to domain.com/blog/index.php
But, still show domain.com/blog/anything-that-is-here.html in the browser address bar.
I hope this makes sense.
Hoping this is something that can be accomplished in.htaccess.
Thanks!
Rick
You are searching for the rewrite function from Apache.
Something like this should work for your case:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?title=$1
An URL like domain.com/blog/test-title will internally call your index.php with $_GET['title'] = "test-title".
Try using [P]
RewriteEngine on
RewriteRule ^(.*)$ / domain.com/blog/index.php [P]
Hello i need to make my url clean and i just do not know were to start as it is mind boggling, i have read numerous things in regards to clean urls but i have no idea.
This what i am getting on woorank as i am doing my seo.
Warning! We've detected parameters in a significant number of URLs.
I am unsure if this is right i have taken my real domain out and put my site instead
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^//(\.)?My site/.*$[NC]
#RewriteRule .(png|gif|jpg)$ – [F]
RewriteCond %{HTTP_HOST} !^ My site.co.uk$ [NC]
RewriteRule (.*) My site.co.uk/$1 [L,R=301]
Thank you J C
A basic htaccess can look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^mysite.co.uk [nc]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [r=301,nc]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1 [L]
For every new parameters you just add ([^/.]+)/ and extra parameter in the end..
What happens in code is up to you, but you will need a standard way of working if you want to use something like this.. .
NOTE: you can't just implement this now, because your site needs to be fully build to the structure of your htaccess.. So if you would replace this now, alot of others things might get broken soon... .
It depends. As Naruto pointed out. We need to know the structure of your code. Give some examples of how the urls are now and what you want them to look like. The examples will explain a bit how you might have programmed the website.
e.g. different php file for each page /about.php, /register.php or a single entry /index.php with every page having the same parameter keys.
/index.php?page=page1&foo=bar&qux=norf
/index.php?page=page2&foo=bar
or perhaps each page has different parameter names
/index.php?page=page1&foo=bar
/index.php?page=page2&qux=norf
What you can always do is redirect to a single index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
index.php now needs to route everything approperiately. This basically does the same as what Naruto suggests but instead in php directly and this might be easier for you.
How this routing happens depends on your code and is up to you. But let us assume that you have a different file for each page with different parameters. You could do this without changing the rest of your code.
// FROM: /shop.php?category=software&subcategory=webdevelopment
// TO: /shop/software/webdevelopment
$path = explode('/',$_SERVER['REQUEST_URI']);
if($path[0] == 'shop') {
$_GET['category'] == $path[1];
$_GET['subcategory'] == $path[2];
include('shop.php');
}
This way only one file needs to be edited and the rest of your code can still work with the $_GET. This is the same as what you would do in your htaccess.
This is the real url:
http://web/app/index.php?id=1
Here I have used current .htaccess and working fine with me.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=$1
Now the url is : http://web.com/app/index/i and working fine
but here is the problem the real url
http://web.com/app/index.php?id=1&name=abc
How can I set mention url with .htaccess for keep it short or any other good solution for hiding URL-.
is it possible to show user only one url like , http://web.com/app but it could go to other pages while the url stay one url static.
If we say, good practice is to rewrite everything to one index page, like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
then in your index.php you get uri by $uri = $_SERVER['REQUEST_URI'] :
http://example.com/app -> $uri = /app
http://example.com/app/sub-app -> $uri = /app/sub-app
but also query string applies to the $uri variable:
http://example.com/app/?a=1&b=3 -> $uri = /app/?a=1&b=3
in this case, if you want to examine just uri part before ? question mark:
$realRri = strtok($uri,'?'); // and you have '/app/' instead of '/app/?a=1&b=3'
next you can examine and manipulate $uri;
Example:
$trimmedUri = trim($realRri, '/');
if ($trimmedUri == 'app')
{
// you can show app page
}
elseif ($trimmedUri == 'contact')
{
// show contact
}
else
{
// show 404 page not found
}
// you can have some dynamic also also
if (is_file($filePath = 'custom-page/'.$trimmedUri))
{
include $filePath;
}
else
{
// 404 page
}
Final Code (regarding your current script)
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
#rewrite php file
RewriteRule ^([a-zA-Z]+)$ $1.php
#rwwrite with id
RewriteRule ^([a-zA-Z]+)/([0-9]+)/?$ $1.php?id=$2
#rewrite with id and name
RewriteRule ^([a-zA-Z]+)/([0-9]+)/([0-9a-zA-Z]+)/?$ $1.php?id=$2&name=$3
I think what you're looking for is something like AJAX, mostly because of
but it could go to other pages while the url stay one url static.
in which case, you would looking at JavaScript and not php. You would need a way to quickly deal with the data and change it how you want it. I've recently started learning Angular, and it shows a lot of promise. Go through the tutorial yourself (just google angular.js and the tutorial is on their website), or some other JavaScript library (unless you want to hard-code everything, in which case, good luck) like jQuery or MooTools (names mentioned because they're libraries i have knowledge with). To my knowledge (I am no code guru) but a website's URI is what tells the server what to show you. You can make shortcuts
If, however, you want for it to just not have the .php filename, then I believe that
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)\.php\??(.*)$ $1.php&$2
Should, theoretically get you what you want.
Note: I can't test it right now, so it may error out.
EDIT: It does error out.
I am trying to change my website URL according to get variables so that I can increase the security of my website.
For example I want to change my address, this is my htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /category.php?cat_id=$1&mode=full&start=$1
And my website URL is:
http://joinexam.in/category.php?cat_id=17&mode=full&start=36
I want to convert this URL to:
http://joinexam.in/category/1736
where cat_id = 17
and start= 36
So it will become 1736 after the category.php page, I am trying to do it by using .htaccess file.
Here I want to take both cat_id and start as get variable, then according to these get variables, I want to change the URL of my website.
Can anyone explain the correct way to achieve this?
.htaccess
This forwards every URL to index.php.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
Get the "original" URL inside index.php:
// this gives you the full url
$request_uri = $_SERVER['REQUEST_URI'];
// split the path by '/'
$params = split("/", $request_uri);
Then you might route based on these $params.
As a good and fast router lib i suggest: https://github.com/nikic/FastRoute
By using this, you don't have to mess around with htaccess regexp stuff and can keep things at the PHP level :)
Are there ways to pass variables in a URL similarly to GET data? For example, with slashes?
I currently have a single .php file which reloads a div with different content using javascript to navigate pages, but as you can imagine, the address in the address bar stays the same.
I want users to be able to link to different pages, but that isn't possible conventionally if there is only one file being viewed.
You're probably going to want to use something along the lines of Apache's mod_rewrite functionality.
This page has a nice example http://www.dynamicdrive.com/forums/showthread.php?51923-Pretty-URLs-with-basic-mod_rewrite-and-powerful-options-in-PHP
Try using:
$_SERVER['QUERY_STRING']; // Or
$_SERVER['PHP_SELF'];
If that doesn't help, post an example of what kind of URL you are trying to accomplish.
Something like this might do the trick;
place this in /yourdir/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ yourindexfile.php?string=$1 [QSA,L]
All requests will be sent to yourindexfile.php via the URL. So http://localhost/yourdir/levelone becomes yourindexfile.php?string=levelone.
You'll be able to break down the string like so;
$query= explode('/', rtrim($_GET['string'], '/'));
the technology your looking for is .htaccess. technically this isn't possible, so you'll have to hack your mod rewrite to accomplish this.
RewriteRule On +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(user)/([^\.]+)$ ./index.html?tab=user&name=$2
add this to your .htaccess page in your top directory. you'll have to alter your website structure a little bit. assuming that index.html is your index. this is a backwards rewrite so if one was to go to the page with the query string it won't redirect them to the former page and if one went to the page without the query string it will work like GET data still.
you GET this data with your php file using $_GET['tab'] and $_GET['name']
I think the Symfony Routing Component is what you need ;) Usable as a standalone component it powers your routing on steroids.
I'm doing it like this (in my like framework, which is a fork of the JREAM framework):
RewriteEngine On
#When using the script within a subfolder, put this path here, like /mysubfolder/
RewriteBase /mysubfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Then split the different URL segments:
$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url, '/');
$url = filter_var($url, FILTER_SANITIZE_URL);
$url_array = explode('/', $url);
Now $url_array[0] usually defines your controller, $url_array[1] defines your action, $url_array[2] is the first paramter, $url_array[3] the second one etc...