.htaccess mod_rewrite to create clean URLs - php

I have a data driven site that passes information to determine what the next page should show using the $_GET parameter.
I want the URL's to look nicer and be structured simply.
I have been reading about mod_rewrite but so far failed to implement it.
<?php $post = $_GET['ID']; ?>
<?php $loca = $_GET['loca']; ?>
This is taken from the URL to work out what table we want and what post ID. The URL at the moment is index.php?ID=4&loca=Pages
How would I make this work if it were instead. /pages/(the name column of the post of this ID).

This should do the internal rewrite:
RewriteEngine On
RewriteRule ^pages/(\d+)/ /index.php?ID=$1&loca=pages
It rewrites any url starting with pages/(some number)/ to the result. You should probably add some server side logic as well to do a 302 redirect if the url isn't exactly /pages/id/(Name that matches id)/. You can use $_SERVER['REQUEST_URI'] to get the string and then compare it to the string that it should be and do a redirect if it doesn't match.
Just like if you go to: https://stackoverflow.com/questions/11057691/This+is+not+the+title
You get redirected to the version with the correct title. You should also update the links you have around your site to use the new url format.

There are a lot of examples of how to do this on google.
Tutorial: http://wettone.com/code/clean-urls
Mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

How do I redirect URL parameter ?m=1?

I recently switched from blogger to wordpress and noticed that many incoming links have added a ?m=1 parameter to the end of my post link.
Example:
http://www.example.com/2015/06/name-of-blog-post.html?m=1
I have searched for a way to take out the ?m=1 parameter and I found a similar situation to mine on this site but the person also had an issue with some links missing .html.
As far as I know, all of my links have .html added on so I don't know that his code would work.
What would be the easiest and best way for me to fix this?
You can't change the incoming links - those are set by the href tag on the page that a user clicks.
It doesn't affect what's on the page unless your page uses that variable, for example in PHP via $_GET:
$data = $_GET["m"];
print $data; //will output "1"
It is usually used in this sense to see where the referrals are coming from - Facebook will append ?ref=ts to the end of outgoing links to show that it was clicked from the "Top Search" for example.
To simply remove the query string entirely when ?m=1 (exactly) is passed as a URL parameter, then you can do something like this in your root .htaccess file using mod_rewrite. The following directives should be put at the top of your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule (.*) /$1? [R=302,L]
Change the 302 (temporary) to 301 (permanent) when you are sure it's working OK. Since permanent redirects are cached by the browser they are not good for testing.
However, if this is simply to resolve a canonicalisation issue (to control the URL that search engines are indexing) then you could simply use a rel="canonical" element in your head section instead.
Also, in Google Search Console (formerly Google Webmaster Tools) you can instruct Google to ignore the m URL parameter. Although this obviously only affects Google.
If you need to match ?m=1 or ?m=0 then you can change the CondPattern from ^m=1$ to ^m=[01]$.

How do I make clean url?

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

Alternative to $_GET

I've a gallery which should display different albums of pictures.
When I open the gallery with the link, I add the album name with PHP:
<a href="gallery/index.php?album=nature
Then I get the album name with $_GET['album']. It works fine.
But for Me is a clean URL important. And when I am finished with the site and upload it to a hoster, then I change the URL "layout" with the .htaccess file.
So the URL http://www.example.com/gallery/index.php?album=blacknwhite change to http://www.example.com/gallery/.
Now I think with the new URL, $_GET doesn't work anymore. Is there a alternative to $_GET to hand over the album name?
Here's again the code sample:
Site with the link:
D
index.php the PHP part:
<head>
<?PHP
$album = $_GET['album'];
?>
</head>
Your rule should look something like:
RewriteRule ^/([a-z]+)/?$ get_product_by_name.php?product_name=$1 [L]
It should not affect $_GET at all, so if it is not working it's because your rule is not setup correctly.
I would recommend you to keep relevant information in your URL, and the album-title seems relevant in this case as I assume the page is centralized around it?
Make your URL work like this instead:
http://www.example.com/gallery/<album-title>/
Since you already seem to know how to use htaccess, rewrite the URL to your "ugly" one there if you get this type of format. Your URL will look way much better then.
http://www.example.com/gallery/blacknwhite/
You need to look at something called htaccess, as you'll need to use RewriteRule which will make it pretty, but still keep $_GET['album'] available in your PHP.
Yes there is another way but its not recommended. look at http://php.net/manual/en/reserved.variables.server.php
you can use $_SERVER. but the correct way is using RewriteRule that well explained here:
http://www.webdeveloper.com/forum/showthread.php?214564-RewriteRule-with-GET-data

.htaccess - need to 301 a URL with special characters

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();
}

using seo user friendly in php

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

Categories