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();
}
Related
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
I want to make a service that allows users to get a direct link to do an action like follow or subscribe (not telling for obvious reasons).
So using htaccess or php (whatever is better), how can I do the following
example.com/insertusernamehere automatically redirects to somepopularsocialmediasite.com/?follow_user=blahblahblah
It's important to note that the example.com site would be on a different server than somepopularsocialmediasite.com.
Also I would like to have some .html pages including an index.html, about.html, etc etc so I would need a way to exclude certain queries/files from redirecting.
I'm assuming you can store the username in a variable somehow, so we could do something like this:
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$username = 'some_username_you_get_somehow';
$url_username = substr(strrchr($current_url, '/'), 1);
if ($url_username == $username)
{
header("Location: http://somepopularsocialmediasite.com/?follow_user={$username}/");
}
If all the pages that will exist on your server (index.html, etc) all have the .html extension, you can simply do this:
RewriteRule ^([a-zA-Z0-9]+)$ http://socialsite.com/?follow_user=$1 [R=301, L]
Basically, that rule will not match anything that contains an extension, because it's only looking for non-case-sensitive letters and numbers. You can even put hyphens in there if you like.
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
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).
Let' say for example one of my members is to http://www.example.com/members/893674.php. How do I let them customize there url so it can be for example, http://www.example.com/myname
I guess what I want is for my members to have there own customized url. Is there a better way to do this by reorganizing my files.
You could use a Front Controller, it's a common solution for making custom URLs and is used in all languages, not just PHP. Here's a guide: http://www.oreillynet.com/pub/a/php/2004/07/08/front_controller.html
Essentially you would create an index.php file that is called for every URL, its job is to parse the URL and determine which code to run base on the URL's contents. So, on your site your URLs would be something like: http://www.example.com/index.php/myname or http://www.example.com/index.php/about-us or http://www.example.com/index.php/contact-us and so on. index.php is called for ALL URLs.
You can remove index.php from the URL using mod_rewrite, see here: http://www.wil-linssen.com/expressionengine-removing-indexphp/
Add a re-write rule to point everything to index.php. Then inside of your index.php, parse the url and grab myname. Lookup a path to myname in somekinda table and include that path
RewriteEngine on
RewriteRule ^.*$ index.php [L,QSA]
index.php:
$myname = $_SERVER['REQUEST_URI'];
$myname = ltrim($myname, '/'); //strip leading slash if need be.
$realpath = LookupNameToPath($myname);
include($realpath);
create a new file and change it name to (.htaccess) and put this apache commands (just for example) into it :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^profile/([0-9]*)$ members.php?id=$1
You must create a rewrite rule that point from http://www.example.com/myname to something like http://www.example.com/user.php?uname=myname.
In '.htaccess':
RewriteEngine on
RewriteRule ^/(.*)$ /user.php?uname=$1
# SourceURL TargetURL
Then you create a 'user.php', that load user information from 'uname' GET variable.
See from your question, you may already have user page based on user id (i.e., '893674.php') so you make redirect it there.
But I do not suggest it as redirect will change the URL on the location bar.
Another way (if you already have '893674.php') is to include it.
The best way though, is to show the information of the user (or whatever you do with it) right in that page.
For example:
<?phg
vat $UName = $_GET['uname'];
var $User = new User($UName);
$User->showInfo();
?>
you need apache’s mod_rewrite for that. with php alone you won’t have any luck.
see: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html