i have this url:
merchantstore.php?merchant=100&product=208
that i want to convert to:
/merchant-store-name/product-name
where merchant-store-name replace ?merchant=100 and product-name replace &product=208
How do i do that in htacess file.
I'd suggest you to use a PHP-based approach. In the .htaccess:
RewriteRule (.*) switchboard.php?orig_uri=$1
This captures the entire requested uri, and kind of forwards it to a central switchboard. Then in the switchboard.php you have access to the requested uri, which you can explode() along '/' signs, then look up the id-s associated with the names in you database.
$components = explode('/', $_GET['orig_uri']);
list( $merchant_name, $product_name ) = $components;
// get merchant id and product id from your database
// and serve suitable content by include-ing:
include 'merchant.php';
This is a simple method, and rather easy to scale. It also has the added advantage that no mod-rewrite magic is required.
Don't forget to add proper error-handling.
Related
I need your help creating some links using mod_rewrite.
I have some pages like:
register.php
login.php
And have the code for them:
RewriteRule ^register/?$ register.php [NC,L]
RewriteRule ^login/?$ login.php [NC,L]
My problem is with "dynamic" links I have since I can't get them working.
For exemple I have links like:
index.php?id=news
índex.php?id=news&article=2
How can I transform those links into:
/news/
/news/article_name
And I have some products (that could have the same name in the same category) but with different ID's like:
índex.php?id=products&p=30
How can I change it to
/products/product-name
After this, is it possible to "generate" an unique name? Since I would like not set in the link the unique ID like products/45342/product-name?
What are the changes I need to make to my code to work with those links?
For example I have links like:
To clarify, you must first change the links in your application to be of the form /news/ or /news/article_name (but see below). You then rewrite these "pretty" URLs back to the underlying filesystem path.
So, to rewrite /news/ back to index.php?id=news you can do something like:
RewriteRule ^(news)/$ index.php?id=$1 [L]
Using the $1 backreference just saves typing. Only use the NC flag if this must be a case-sensitive match, but note that this potentially creates duplicate content, so you must specify the canonical URL in some other way (eg. rel="canonical" link element). For the same reason, only make the trailing slash optional if this is a specific requirement.
However, it's not possible to rewrite /news/article_name back to index.php?id=news&article=2 (I assume that should be i, and not í, as in your question?) since the article ID (ie. 2) is not present in the source URL. You need to include the ID in the source URL (or make the article_name unique and a key in your lookup). It would be more usual to create a URL like /news/2/article_name (which is what StackOverflow does), which can be easily rewritten. The article_name in the URL is purely for users (and indirect SEO). In which case you could rewrite this like so:
RewriteRule ^(news)/(\d+)/ index.php?id=$1&article=$2 [L]
This will rewrite /news/N/<anything> to /index.php?id=news&article=N (where N is 1 or more digits).
However, since it rewrites <anything> you should also implement a redirect in your application when the non-canonical article_name is accessed. (Which again, is what StackOverflow does.)
And I have some products (that could have the same name in the same category) but with different ID's like: índex.php?id=products&p=30
How can I change it to /products/product-name
The same principle as mentioned above applies here also.
After this, is possible to "generate" an unique name?
You can generate this "unique name" in your application, not .htaccess. Build you URLs in your application etc.
Since I would like not set in the link the unique ID like "products/45342/product-name" ?
As mentioned above, either your product-name is unique, and behaves like your id. Or you incorporate the unique ID in the URL - this is the far more common approach, offers greatest flexibility and is less prone to error. A "short" URL like /products/45342 will redirect you to the correct canonical URL.
I have my own website and I want this like stackoverflow have
When you look at the url there is a nice url like: https://stackoverflow.com/questions/40587870/javascript-variable-in-bootstrap-tooltip
When I only enter https://stackoverflow.com/questions/40587870 it send me to the full url.
This is what I have now: http://www.exmaple.com/projects.php?id=1
How can I make this on my own website?
You need some sort of database for this, which maps an id to its appropriate slug. This could be as simple as a text file containing the mappings, e.g.
41960970 htaccess-url-rewriting-id-and-name
40587870 javascript-variable-in-bootstrap-tooltip
You can then define a RewriteMap
RewriteMap id2slug txt:/path/to/file.txt
and then use it in a rule
RewriteRule ^questions/([^/]+)$ /questions/$1/${id2slug:$1} [R,L]
This rule captures the id from the request and then redirects the client to the same URL with a slug appended.
Instead of a text file, you can also use other forms like dbm, dbd, or even an external program providing the mapping.
Be aware though, that RewriteMap is only available in the main server or virtual host configuration.
When you don't have access to the main config, you might simulate this with a script doing the redirect, e.g.
RewriteRule ^questions/([^/]+)$ /id2slug.php?id=$1 [L]
and in the script, retrieve the slug from a database and send back
$id = $_GET['id'];
$slug = ...;
header('Location: http://www.example.com/questions/' . $id . '/' . $slug);
I have an url that looks like:
/platforms.php?platform_id=xxx
where xxx is a number
I'm rewriting the URL inside the php application. So, for example the above url would look like:
/xbox/ or /playstation/
Now in .htaccess I have:
RewriteRule ^([^/]+)/$ platforms.php?platform_id=$1 [L,QSA]
However when I go to a platform page the GET url becomes /xbox/ or /playstation/ , instead of xxx.
Any pointers would be appreciated.
Update:
Hi, the link is not relevant to my question. I've tried to reformulate what I am after for in the example bellow with better details.
Thanks for the answer and sorry for the bad explanation.
Yep, when I said GET url I was referring to $_GET["platform_id"] .
Basically I have an URL called
www.example.com/platforms.php?platform_id=1
In the above example $_GET['platform_id'] = 1.
In the actual php aplication I have a function (let's call it make_link ), with which I make the above URL output like:
www.example.com/xbox/ (since 1 is the id of the xbox platform)
Now in httaccess I also need a rewrite rule that will make accessing the URL work.
So I have :
RewriteRule ^([^/]+)/$ platforms.php?platform_id=$1 [L,QSA]
This does make the rewrite work in the terms that I can access
www.example.com/xbox/
However on the newly accessed page, if I get $_GET['platform_id'], the value for it is xbox/ .
Thanks,
In a RewriteRule, $1 is a variable backreferencing the first regular expression (in your case: ([^/]+)).
So, whatever text forms that part of your URL is what will be stored in $1.
If you wanted to use the consoles' IDs, you'd have to make these IDs part of your URLs. If you don't want that, but if you want your pretty URLs to reflect the names of the consoles, you'll have to rewrite the query part of platforms.php?platform_id=$1 in your .htaccess file.
Instead of querying for IDs (?platform_id=$1), you'll have to query for the consoles' names, e.g. ?platform_name=$1.
Edit:
In your PHP file, you'd then use $_GET['platform_name']
I have a rule in my .htaccess file to overwrite ugly parameter urls with clean paths.
Here is an example:
RewriteRule ^landing(/)?([0-9]+)?(/)?$ landing/index.php?intPage=$2
So when someone goes to /landing/3, it would load the index.php page passing intPage parameters as 3.
Very simple.
However, I'm using techniques to trace Google Analytics referals, so I need to tack parameters onto this URL:
For example:
/landing/3?kt_type=ad&kt_st1=adwords&kt_st2=prize_a_us.en
The problem is that, I don't know how to retrieve the parameters that are tacked onto the URL, since the URL has already been overwritten and now I can't retrieve kt_type, kt_st1 etc.
Any idea on how to make it possible so I can still tack parameters to already overwritten URLs?
Use QSA flag. Change your rule to this:
RewriteRule ^landing/?([0-9]+)?/?$ landing/index.php?intPage=$1 [L,NC,QSA]
From official docs:
QSA - Appends any query string from the original request URL to any
query string created in the rewrite target
I have a problem with query strings. I want to change this...
http://www.audiomasterclass.com/arc.cfm?a=giant-killing-%245-mic-preamp-its-secrets-reveale
To this...
www.audiomasterclass.com/?a=588
Based on this url it can't be done. You need logic to reverse the name to a ID.
What you can do is make sure that in the original url the ID is included. For instance:
http://www.audiomasterclass.com/arc.cfm?a=giant-killing-%245-mic-preamp-its-secrets-reveale
can be changed to: http://www.audiomasterclass.com/588/giant-killing-%245-mic-preamp-its-secrets-reveale
And then use a rewrite rule:
RewriteRule ^/([0-9]+) /arc.cfm?a=$1 [L]
What you do is add the id to every link so you system will on basis of ID but google/users see the nice URL.
You need to change the rewrite rule a bit (for your needs and server config) but it will explain the basics.