I want to make the clean url for my site. How do I change htaccess file. My original Link that
This is my original link
**http://www.tangailbazar.com/adview_details.php?ID=9014&show=Hot%20and%20Cool%20Water%20Filter**
I want to make it like this
http://www.tangailbazar.com/Hot-and-Cool-Water-Filter
My code is here.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /^(.*) adview_details.php?show=$1 [PT,QSA]
My post link from where i get the value like this
View Details
In adview_details page I have written the code
<?php
$id=$_GET['ID'];
$id1=$_GET['show'];
?>
<?php
$SQL="select * from tb_classified where sl='$id' and title='$id1'";
$obj->sql($SQL);
while($row = mysql_fetch_array($obj->result))
{
echo $row['title'];
echo $row['description'];
}
?>
You are looking for an Apache mod called mod_rewrite. Specifically (from the documentation):
If, on the other hand, you wish to pass the requested URI as a query string argument to index.php, you can replace that RewriteRule with:
RewriteRule (.*) adview_details.php?show=$1 [PT,QSA]
Note that these rulesets can be used in a .htaccess file, as well as in a block.
<?php
$title = str_replace(' ', '-', $row['title'])
?>
View Details
Output URL http://www.tangailbazar.com/Hot-and-Cool-Water-Filter
but this will not work, why? because you can not make a url with a condition, while for the request to mysql need two conditions.
maybe you should change your url into http://www.tangailbazar.com/1/Hot-and-Cool-Water-Filter
If you follow my advice you should change the code. htaccess becomes
RewriteRule ([0-9]?)/([a-zA-Z_-]+) adview_details.php?ID=$1&show=$2 [PT,QSA]
and the URL that you should use
View Details
First what draws my attention is that your regular URL is not as clean to start with. I would advise you to remove the spaces and change them for hyphens—if possible. URL are also case insensitive so there is actually no use for uppercase characters, but using them is personal preference though it makes them less readable and when rewriting it's an extra process to change them.
When rewriting a URL, you should think of your URL as parts. There is the domain which is divided in: subdomain (www), domain-name (tangailbazar) and the top-level domain (com). After the domain begins the cleaning up (you can also do a lot of rewriting on the domain, but in your case you will not).
To keep your rewrite process clear and simple (because URL rewriting with mod_rewrite can give massive headaches) you want every part of a rewritten URL (between slashes) to translate to a parameter in the raw URL. So in your case: ID=9014&show=Hot and Cool Water Filter are two parameters that should be used in your clean URL. When rewriting the URL you would pick the value of key (parameter) ID and show and use them in your clean URL. In your case you need the ID parameter in the clean URL otherwise you can never not load your destination URL.
RewriteRule ^.+/(.+)/(.+)+$ adview_details.php?ID=$1&show=$2 [L]
This rule will change the request for:
www.tangailbazar.com/9014/Hot%20and%20Cool%20Water%20Filter
into:
www.tangailbazar.com/adview_details.php?ID=9014&show=Hot%20and%20Cool%20Water%20Filter
This is the best you can do in this case, otherwise you will have to change some things about the structure of the destination URL.
An important thing you need to get hold of is that the name URL rewriting is misleading, you actually don't rewrite anything, you translate the requested URL from the browser into the URL where you're page is located. So it's more of a 'URL translate' then a 'URL rewrite'. In creating user friendly URL's you most of the time are removing the parameter keys (show=) and file extension (.php) from the URL to make them more readable.
You will always need to use the dynamic parts in your URL and you can clean the static parts.
I hope this will help you solve your problem or at least make some things clear on rewriting URL's.
Good luck!
httpd.apache.org/docs/current/mod/mod_rewrite.html
www.regexr.com
Related
I am trying to use mod_rewrite to redirect users keeping the multiple query string and creating a redirect page
For Example,
If user opens
http://localhost/url/url/http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
then he is taken to
http://localhost/url/url.php?redirect=http://www.google.com/contacts/?user=abc&stackoverflow=great&google=facebook
There is secondary problem that URL should be encoded and then redirected! If URL is not encoded then the string (&stackoverflow=great)would be not a part of 'redirect' string of url.php
I tried many solutions then came for stackoverflow! I tried the following code in following file
http://localhost/url/.htaccess
RewriteRule ^url/([^/])$ url.php?redirect=$1 [QSA,L]
but the result is localhost/url/url.php?redirect=http only
Your setup won't work with the unencoded inner url, so an 'answer' can only have temporary character. But this might be a starting point:
RewriteEngine on
RewriteRule ^/url/url/(.*)$ /url/url.php?redirect=$1 [L,QSA]
I wonder however if that fragment /url/url is really intended (the two 'url's in there).
Note that the exact rule content also depends on where you want to define that rule. The syntax is different whether you use the central server configuration (referred) or .htaccess style files (as second choice and more complex).
Try this
RewriteEngine on
Redirect ^url/url/(.*)$ url/url.php?redirect=$1
The basic redirect systax,
redirect accessed-file URL-to-go-to
I would like to make my urls more seo friendly and for example change this:
http://www.chillisource.co.uk/product?&cat=Grocery&q=Daves%20Gourmet&page=1&prod=B0000DID5R&prodName=Daves_Insanity_Sauce
to something nice like this:
http://www.chillisource.co.uk/product/Daves_Gourmet/Daves_Insanity_Sauce
What is the best way of going about doing this? I've had a look at doing this with the htaccess file but this seems very complicated.
Thanks in advance
Ben Paton, there is in fact a very easy way out. CMSes like Wordpress tend to use it instead of messing around with regular expressions.
The .htaccess side
First of, you use an .htacess with the content below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Let me explain what it does (line by line):
if the apache module named mod_rewrite exists..
turn the module on
let it be known that we will rewrite anything starting after the
domain name (to only rewrite some directories, use RewriteBase
/subdir/)
if the requested path does not exist as a file...
and it doesn't even exist as a directory...
"redirect" request to the index.php file
close our module condition
The above is just a quick explanation. You don't really need it to use this.
What we did, is that we told Apache that all requests that would end up as 404s to pass them to the index.php file, where we can process the request manually.
The PHP side
On the PHP side, inside index.php, you simply have to parse the original URL. This URL is passed in the $_SERVER variable as $_SERVER['REDIRECT_URL'].
The best part, if there was no redirection, this variable is not set!
So, our code would end up like:
if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
$url = explode('/', $_SERVER['REDIRECT_URL'] );
switch($url[0]){
case 'home': // eg: /home/
break;
case 'about': // eg: /about/
break;
case 'images': // eg: /images/
switch( $url[1] ){
case '2010': // eg: /images/2010/
break;
case '2011': // eg: /images/2011/
break;
}
break;
}
}
Easy Integration
I nearly forgot to mention this, but, thanks to the way it works, you can even end up not changing your existing code at all!
Less talk, more examples. Let's say your code looked like:
<?php
$page = get_page($_GET['id']);
echo '<h1>'. $page->title .'</h1>';
echo '<div>'. $page->content .'</div>';
?>
Which worked with urls like:
index.php?id=5
You can make it work with SEO URLs as well as keep it with your old system at the same time. Firstly, make sure the .htaccess contains the code I wrote in the one above.
Next, add the following code at the very start of your file:
if ( isset( $_SERVER['REDIRECT_URL'] ) ) {
$url = explode('/', $_SERVER['REDIRECT_URL'] );
$_GET['id'] = $url[0];
}
What are we doing here? Before going on two your own code, we are basically finding IDs and information from the old URL and feeding it to PHP's $_GET variable.
We are essentially fooling your code to think the request had those variables!
The only remaining hurdle to find all those pesky <a/> tags and replace their href accordingly, but that's a different story. :)
It's called a mod_rewrite, here is a tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
What about using the PATH_INFO environment variable?
$path=explode("/",getenv("PATH_INFO"));
echo($path[0]."; ".$path[1] /* ... */);
Will output
product; Daves_Gourmet; Daves_insanity_Sauce
The transition from using $_GET to using PATH_INFO environment is a good programming exercise. I think you cannot just do the task with configuration.
try some thing like this
RewriteRule ^/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+) /$1.php?id1=$2&id2=$3 [QSA]
then use $_GET to get the parameter values...
I'll have to add: in your original url, there's a 'prod' key, which seems to consist of an ID.
Make sure that, when switching to rewritten urls, you no longer solely depend upon a unique id, because that won't be visible in the url.
Now, you can use the ID to make a distinction between 2 products with the same name, but in case of rewriting urls and no longer requiring ID in the url, you need to make sure 1 product name can not be used multiple times for different products.
Likewise, I see the 'cat'-key not being present in the desired output url, same applies as described above.
Disregarding the above-described "problems", the rewrite should roughtly look like:
RewriteRule ^/product/(.*?)/(.*?)$ /product?&cat=Grocery&q=$1&page=1&prod=B0000DID5R&prodName=$2
The q & prodName will receive the underscored value, rather than %20, so also that will require some patching.
As you can see, I didn't touch the id & category, it'll be up to you to figure out how to handle that.
I have a long URL from an legacy website which I need to 301, example: domain.com/web/vehicle/655520/2007-Hummer-H2---?sort_by=year&args=All_years--All_makes--All_models--All_body_types--All_vehicles
I need to redirect this (and many more similar urls) to a new page on a redesigned website, page example: domain.com/hummer.php
How do you strip the special characters (ex. ---?) and everything else from the URL so that I can successfully use a 301?
You can't "strip" anything with mod_rewrite.
You can only create references from parts of a string and use them for building the new url.
How you can do it depends on what url you like to build out of the original url.
Why do you need to? Unless you're planning to code a long, long list of redirects into your .htaccess file, you should be doing all of your redirects in PHP.
From the URL example you gave, I assume all items have a unique ID that is tied to the URL already. In that case, you could create a map in your database that says that the "proper" URL for item 655520 is hummer.php. You can use that to perform a redirect from PHP.
Here's an example of how you can do this. I'm making the assumption that you already have an .htaccess file which translates the URL into a GET. Something like RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
//determine if you were passed a "legacy URL" (not shown)
if (legacyURL) {
$urlComponents = $explode("/", $_GET['request']);
$url = getItemUrl($components[2]);
header("Location: " . $url,TRUE,301);
exit();
}
Hey guys - im using var passing links like this to jump around a site...
<a href = "index.php?content=about.html">
...problem is, i have all the ugly var info visible in the url. I would usually hide it by using the post method, but i dont have any form tags, so is it even possible?
Thanks!!!!!!!
It's a bad idea. GET is used for reading, POST is used for updating. A better solution would be to use some sort of mod_rewrite to make friendly URLS. Often called SEO friendly URLS...
Yes, you can POST with a <a href... but you have to have a lot of ugly javascript to do it... which breaks all sort of standard conventions.
Update, combining some new information
#FDisk has the simplest solution below, but I would add a condition to it which would allow existing files to be passed through directly by the webserver without having to run it through PHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
That way, a request to /images/bar.png will be served directly from the filesystem if that image exists.
Note, that your page does not necessarily need to have ".html" on the end anymore. So your URL could look like: http://example.com/about which would then be converted to: index.php?content=about
Taking it one step further from the link you listed in your post, you could then parse the url for various parameter. The example you looked up stuffed them into [$_GET, $HTTP_GET_VARS, $_REQUEST] respectively, but I think that's not such a good idea. Just make your own array of parameters.
You can try using the mod_rewrite extention
The original URL:
http://www.youwebsite.com/index.php?content=about.html
The rewritten URL:
http://www.youwebsite.com/about.html
.haccess file content:
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
you can hide info (var name and content) by encoding it. Thus the user won't be able to understand or change what you are passing around. But he will still see something in his url.
I guess you should give use some more context to understand why you cant use direct links to static pages ?
this is the URL path I currently use:
/index.php?page=1&title=articles
I want to get the URL path as
/index/page-1/title-articles
using SEO user friendly URLs in PHP.
And how to get the value of the "title"? Any one can help me pls.
Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.
You need to ensure two things:
your application prints out the new URLs properly, and
your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.
The first part can be simply accomplished by using
echo ' … ';
instead of
echo ' … ';
The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:
RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]
The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.
I want to get the URL path as
/index/page-1/title-articles
Why? I’ve got two objections:
index is a no-information and I doubt that it belongs in the URI
page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.
Thus, I propose either:
/‹article›/1
or
/‹article›/page/1
or
/‹article›/page=1
or
/‹article›[1]
or
/articles/‹article›/page/1
Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).