I am not sure if this is possible, but I have the following URL that I want to make more friendly:
http://www.myurl.com/content.php?id=13089093057550&slug=this-is-a-nice-url
And I want it to look like this:
http://www.myurl.com/this-is-a-nice-url
How do I do this?
You'll need to pass the id to your PHP code in order to display content I believe. For example, look at the Stack Overflow URL for this question: http://stackoverflow.com/questions/6520671/htaccess-rewrite-friendly-urls where it is passing both id and slug. So you can have friendly URLs like:
http://www.myurl.com/13089093057550/this-is-a-nice-url
If you decide to go by my suggestion then here is the code you will need in .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteRule ^([0-9]*)/(.*)/?$ /content.php?id=$1&slug=$2 [L,QSA]
You can get the slug into the querystring, but without providing the id somewhere it's impossible for Apache to supply it. Hopefully you have a way to get the id out of your database using only the URL slug parameter.
RewriteEngine On
# We don't know the id
RewriteRule (.*)$ /content.php?slug=$1 [L,QSA]
Related
www.example.com/tour_detail/angkor-songsaa-luxury-private-island-honeymoon-premium-tour-19-105.html
Note : 19-105 is id, so i don't want it show like that i just want
www.exmaple.com/tour_detail/angkor-songsaa-luxury-private-island-honeymoon-premium-tour
Put this in your .htaccess:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^angkor-songsaa-luxury-private-island-honeymoon-premium-tour/?$ angkor-songsaa-luxury-private-island-honeymoon-premium-tour-19-105.html [NC,L] # Handle requests for your url
If you want to read more: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
this is my first time to try .htaccess
this is what i want.
example.com/account/blitzen12 -> example.com/account/index.php?id=10&name=blitzen12
i don't want to include the id in rewriting, is it possible?
Note: id and name which is 10 and blitzen12 is retrive from the database.
so far this is what I've tried but it didn't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^account/(.*)$ ./account/index.php?page=account&id=$1&name=$2 [L,NC]
html code.
blitzen12
can anyone help me with this? Thank you.
The "10" or the id, isn't part of the URL:
example.com/account/blitzen12
So you can't rewrite it into another URL, can't pull it out of thin air. You'll either need to just serve the page without an "id" (and pull it out of the database using the "name") or embed it in the URL without the query string, something like:
example.com/account/10/blitzen12
then you'd be able to rewrite it using:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^account/([0-9]+)/(.*)$ ./account/index.php?page=account&id=$1&name=$2 [L,NC]
I have a url that looks like this
http://mysite.com/item/?food=1&drink=1&bar=1&name=Bomba
I want to make it more friendly and maybe more secure and to look something like
http://mysite.com/item/Bomba
The problem is that sometime drink or bar will not be part of the url, so I don't know how to make it to work with .htaccess. Also I don't know how to make rules for multiple get variables and if I can use conditionals in a rewrite rule (if drink==true or something similar).
And also I don't want to use post because I want to be able to share the link.
So far I made something like this
mysite.com/item/1/Bomba.menu
and the rule
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^item/(.+)/(.+).menu item/?food=$1&drink=$1&bar=$1&name=Bomba [nc]
But it only works if the url stays the same.
Thanks
So at last I made this short link http://mysite.com/item/1/D1/W1/Bomba that is taking me here http://mysite.com/item/?food=1&drink=1&wine=1&name=Bomba
And added this rules to the .htaccess file.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^item/(.+)/(D.)/(W.)/(B.+)/(.+) item/?food=$1&drink=$1&wine=$1&bar=$1&name=$5 [nc]
RewriteRule ^item/(.+)/(D.+)/(W.+)/(.+) item/?food=$1&drink=$1&wine=$1&name=$4 [nc]
RewriteRule ^item/(.+)/(D.+)/(B.+)/(.+) item/?food=$1&drink=$1&bar=$1&name=$4 [nc]
RewriteRule ^item/(.+)/(W.+)/(B.+)/(.+) item/?food=$1&wine=$1&bar=$1&name=$4 [nc]
I hope that it will help somebody.
:)
[EDITED Answer]
You would need to have them set as key/value pairs as you have no way to tell if is a Drink or a Bar ID.
The other way I can think of, which won't look quite as elegant is to prefix the ids, so if it was a Drink ID then make the dynamic URL:
http://mysite.com/item/D1/B4
Then if it is prefixed with 'D' then it is a drink, 'B' for bar etc
I am currently coding a pagination script into many parts of my site, this has been a well needed and requested feature and I have finally been able to come round and start coding it, it is all going well, until I find that my rewritten urls don't like working with the pagination urls.
So, an example page on my site would be news.php. This file structure can be something like news.php?id=5. I have rewritten the url like so:
/news/5/
## Rewrite URL's for News & Dev ##
RewriteRule ^news/([0-9]+)/$ /news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)$ /news.php?id=$1 [L]
RewriteRule ^news$ /news.php [L]
RewriteRule ^news/$ /news.php [L]
The pagination script I am using prepends two new variables in the url, the new url turns out to be this:
news.php?page=1&ipp=55id=5
I would really appreciate it if anyone could assist me in making the urls look better, as it defeats the object of having it in the first place if after they use the pagination, it changes the url back to a clunky and ugly url.
I don't want it to be required to have parts of the url, that is something I really don't want..
e.g I don't want the url to be required to be /news/1/55/5, instead id like it to be optional.
Thank you for your time, it is appreciated!
Additional Information
The links in my news script currently display like so:
news.php?page=1&ipp=55id=5
I don't like to see ugly urls like that, and want to make the url look better using mod_rewrite, It would be better if the urls would display like so:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
Also, to make it as user friendly as possible, I don't want any of the fields to be required as such, so for example I would like to have the following link accessible at all times without it requiring the other fields.
/news/ID/
Then, when the user clicks a pagination link, it would use the following link structure:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
This is all from user feedback of my site, and is something that people have been asking for. Problem is, I don't understand even simple .htaccess
Thanks
RewriteBase /
# add slash to end of url if not present (and do a redirect)
RewriteCond $0 !/$
RewriteRule ^news([^\.]*)$ $0/ [L,R=302]
# rewrite url with format /news/[<id>/[<page>/[<ipp>/]]]
RewriteRule ^news/(?:([0-9]+)/)?(?:([0-9]+)/)?(?:([0-9]+)/)?$ /news.php?id=$1&page=$2&ipp=$3 [L]
Not sure what ipp is supposed to be, but my guess is it shows the number of item per page. I would personally not like to have that in my url.
You can have :
news/id/page/ipp with
RewriteRule ^news(/?)$ news.php [L]
RewriteRule ^news/([0-9]+)-(.*)_([0-9]+)(/?)$ news.php?page=$1&ipp=$2&id=$3 [L]
news/1222-subjet-for-example_34
return :
news.php?page=1222&ipp=subject-for-example&id=34
use (/?) instead of create many rules ;)
Hope it's works for you.
I am wondering how would I change the url from:
single.php?id=1
to:
quote-1
Each submitted data has an id, of course. They can go to the URL, the id of the "quote" to get the quote. How would the URL rewrite look? I've tried to use tools online, but ether they didn't work or I didn't understand how to use it.
Place the following your .htaccess file:
RewriteEngine on
RewriteRule quote-(\d+) single.php?id=$1 [QSA]
mod_rewrite documentation