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);
Related
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 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.
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.
My site currently displays like this (when clicking on an ad):
hxxp://mysite.com/viewItem.php?id=10
I would LOVE for the urls to read:
hxxp://mysite.com/dogs/121/border-collie-for-sale-to-good-home
where "dogs" is forever a constant, 121 is always the $row['postId']; and "border-collie-for-sale-to-good-home" is always my $row['title'];
I know it probably won't be so easy as a quick answer here but would appreciate it if you could get me going in the right direction. I'm assuming a change to .htaccess is in order and I am just terrible at trying to decipher that.
Something that you can do is add a redirect at the top of the viewItem.php script. This redirect will need to check for a query parameter that you use to indicate that the .htaccess file as rewritten. Something like this:
if( !isset($HTTP_GET_VARS['rewritten']) ) {
// use whatever behind the scenes stuff you need to construct the friendly URL
$friendly_url = "http://mysite/" . getCategory() . "/" . getID() . "/" . getTitle();
// Now redirect the browser:
header("HTTP/1.1 301 Moved Permanently");
header("Location: $friendly_url");
exit();
}
// business as usual, the rest of your viewItem.php script.
So when this php script tries to handle a request that DOESN'T have the rewritten parameter in the query string, it redirects to the friendly version of the URL. If it DOES have rewritten it does what it usually does and handles the request.
Now in the .htaccess file, you want to rewrite the ugly URL to the friendly URL, but you need to include rewritten in the rewrite (I assume the "121" in your example is the "id" that the viewItem.php script takes:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/([^/]+)/ /viewItem.php?id=$1&rewritten=true [L]
If you leave out the &rewritten=true there, it's going to go into a redirect loop. You can also add a QSA in the brackets of the rule if you want to pass other query strings along with the rewrite in case you want to be able to handle friendly URLs like: http://mysite/dogs/121/border-collie-for-sale/?sort=asc With [L,QSA], the "sort=asc" gets passed along to viewItem/php.
So this is what happens now when someone clicks on an ad and gets taken to http://mysite/viewItem.php?id=121:
The browser location bar says http://mysite/viewItem.php?id=121 request is sent to mysite and viewItem.php is accessed
The top of viewItem.php sees that there is no rewritten param, so it redirects the browser to http://mysite/dogs/121/border-collie-for-sale
Since that was a 301 redirect, the browser's location bar changes to http://mysite/dogs/121/border-collie-for-sale
Request is resent to mysite but this time, the .htaccess rewrites /dogs/121/border-collie-for-sale to /viewItem.php?id=121&rewritten=true INTERNALLY, so the browser's location bar does not change.
The request makes it back to viewItem.php, this time, the script sees that there is a rewritten parameter, and does it's usual business and the request is served
You can use whatever name for the parameter flag you want, as long as none of the scripts are using it.
Can you please tell me how I can redirect the url
like www.santaraj.com/steve to a new my file profile.php?
Actually I want to display the user profile by profile.php but the url will remain as domain name/username (www.santaraj.com/steve). A good example is www.twiiter.com/kk.
Put the following rule in .htaccess in your document root:
RewriteRule ^([a-z0-9]+)$ /profile.php?user=$1 [L]
(assumes a 'RewriteEngine on' in the same file)
When a user goes to www.santaraj.com/steve, your profile.php script will be run, with a query string of user=steve.
Note how the regular expression in the rule assumes that user names are composed of numbers and lower case letters. You can change that to whatever makes sense for you, of course.