How to rewrite a url with id and title passed - .htaccess

Basically, what i want is this:
For example, I have a url that looks like this :
example.com/stories/show.php?id=$id
I want that url to look like this
example.com/stories/$id/$title
And I want the title to look like this:
I love stackoverflow to
I-love-stackoverflow
Seperating each words with a dash.
Am I supposed to pass the title along with the id??
e.g example.com/stories/show.php?id=$id&title=$title
And how do I achieve all this with .htaccess ??
A good example of what I want to do is used by stackoverflow.. Check the url of this question.

You only need some simple rule like in the below example
RewriteEngine on
# rewrite stories/<id>/<title> to show.php
RewriteCond %{REQUEST_URI} stories/([0-9]+)/([a-z\-A-Z]+)
RewriteRule (.*) show.php?id=%1&title=%2 [L]
Place the above in your .htaccess file, which should be located under the stories folder. Of course you also need to make sure so that the rewrite module is enabled on your server. This could be done by running the following commands from the terminal:
sudo a2enmod rewrite
sudo apachectl restart
Now you should be able to rewrite an url like:
http://example.com/stories/666/stack-overflow
to:
http://example.com/stories/show.php?id=666&title=stack-overflow
Then you could create your links in the following manner:
<?php
// php example
$title = "I love stackoverflow";
$id = 666;
?>
link
I think you should read up on how url rewriting really works, here is a couple of links that might be useful:
URL Rewriting for Beginners
URL Rewriting Guide

Related

remove middle part of query from url with htaccess

searched on stackoverflow a bit, but nothing helpfull.
I want to do the following via htaccess.
I have this url:
site.com/location/usa/ca/los-angeles
and I want this:
site.com/location/ca/los-angeles
Basicly I want to remove the abbreviation of the country. Keep in mind, I have more then one country.
Any ideas?
To change all link like /location/1/2/3 to /location/2/3.
Use in your : /location/.htaccess:
RewriteEngine on
RewriteRule ^[^/]+/([^/]+/[^/]+)/?$ $1 [R=301,L]

Is it possible to do http://www.something.domainname.com where something could be a variable?

I would like for any user come to redirect to their page on my site after they login with .htaccess.
For example, "username" login and redirects to:
http://www.username.domainname.com
That will be handled internally like this:
http://www.domainname.com/users.php?user=username
Yes it is by doing a redirect.
If you are writing in PHP, you can do this:
<?php
$username = $_GET["user"]; // Note that this is user input (which could be malicious)
header("Location: http://www.{$username}.domainname.com/");
?>
But, as you have not specified a language, that is just one example. You can easily search for how to do a redirect in whatever framework you are using and there should be good examples.
If you want to go the other way, try a URL Rewrite.
Here's a very basic one that should work:
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domainname\.com$
RewriteRule ^.*$ http://www.domainname.com/users.php?user=%1 [L,R]
The rule looks for any request that matches www.(something other than .).domainname.com and sends back a redirect request for http://www.domainname.com/users.php?user=(something other than .)

.htaccess how to create an URLs like http://example.com/~someuser

How do I can create and process an URLs like this via .htaccess rules: http://example.com/~someuser
And then get a parameter "username" as a part of $_GET array. Thanks in advance.
Something like this:
RewriteEngine On
RewriteRule ^~([^/]+)/?$ /script.php?user=$1 [L]
If those rules are placed in the htaccess file in your document root, then it'll capture the "someuser" part of the URL and rewrite it to the script.php script and set it as the $_GET['user'] variable. You can replace script.php with whatever name of the script you want the variable to go to.

want to replace .php?url= this part by / from my url using .htaccess

In my website there is a link like this
http://backlinks.cheapratedomain.com/backlinks.php?url=emobileload.com
for better SEO I want to make it like this
http://backlinks.cheapratedomain.com/backlinks/emobileload.com
just want to replace '.php?url=' by '/'
My question is how can i do this by editing my .htaccess file ?
If it's only for that one URL, then you could try this:
RewriteRule ^backlinks/([a-zA-Z0-9\.\-]*)$ backlinks.php?url=$1 [L]
So requested URLs like http://backlinks.cheapratedomain.com/backlinks/domain.com will be redirected to http://backlinks.cheapratedomain.com/backlinks.php?url=domain.com

want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 part by /backLinks_free_Backlinks/1 from my url using .htaccess

i have a link look like this
http://backlinks.cheapratedomain.com/index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7
want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 this part by /backLinks_free_Backlinks/1 using .htaccess
after replacing link will be look like this
http://backlinks.cheapratedomain.com/backLinks_free_Backlinks/1
Try adding this to your htaccess file in the document root:
RewriteEngine On
RewriteRule ^backLinks_free_Backlinks/([0-9]+)$ /index.php?backLinks_free_Backlinks=$1&totalRows_free_Backlinks=7 [L,QSA]
You can remove the &totalRows_free_Backlinks=7 bit from the target if you don't need it.

Resources