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]
Related
Hello everyone wondering if i can pick your brains and il be as descriptive as possible.
So i am trying to create a page that will load a title and category (dynamic from database) based upon the selection that is picked by the user
sitename used as my domain name
So to be a little clearer i have a file and variables named sitename/2017/slot.php?gp=category&sn=title
the category and title are stored in the database for each row what i want to do is make a clean URL to look like this sitename/2017/slot/category/title/
So when viewing the page like this sitename/2017/slot.php?gp=category&sn=title the page loads everything in and displays correctly and the variables are being set as i can see them in my title in the browser tab
my error comes when i use the mod rewrite to make it clean, once the .htaccess has been changed and uploaded and i navigate to the new clean URL sitename/2017/slot/category/title/ it shows me the variables havent been set as they no longer appear in the title tab of the browser and also the page doesnt use my css. Below i have added my htaccess rule, any help and feedback is greatfully appreciated
#change this sitename/2017/slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
just incase here is the php code im using in slot.php i am connecting to my database fine no issues there and after the code below im running a while loop to get out the data for the row the user clicked on
if($_GET['gp'] && $_GET['sn']){
$gp = preg_replace('#[^a-zA-Z-]#i', '', $_GET['gp']);
$sn = preg_replace('#[^a-zA-Z-]#i', '', $_GET['sn']);
}
here is the full code i have in my htaccess
#make custom page not found
#ErrorDocument 404 /filenotfound.php
#stop directory file listing in all folders
IndexIgnore *
#make pages have www. in them
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.org
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent,L]
#change this slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
Options -MultiViews +FollowSymLinks
RewriteRule ^slot/([a-z-]+)/([a-z-]+)/ /2017/slot.php?gp=$1&sn=$2 [R=302]
If your URL starts with 2017 you must include it in your RewriteRule.
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
You're misusing the ^, which matches the very start of the path (the part after sitename/. That is, a rule starting with ^slot/ will match only sitename/slot/, not sitename/2017/slot/. Your rule should look like this:
#change this sitename/2017/slot.php?gp=category&sn=title
#to this sitename/2017/slot/category/title/
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ slot.php?gp=$1&sn=$2 [NC,L]
May I join the party?
RewriteRule ^sitename/2017/slot/([a-z-]+)/([a-z-]+)/ /slot.php?gp=$1&sn=$2 [NC,L]
If the sitename the domain name, I will change my answer to :
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ /slot.php?gp=$1&sn=$2 [NC,L]
Which is almost the same as Dekel's, but not exactly :)
#example.com/2017/slot.php?gp=category&sn=title
#example.com/2017/slot/category/title/
You'll need to disable MutliViews. Near the top of your .htaccess file:
Options -MultiViews
With MultiViews enabled (part of mod_negotiation) /2017/slot is being implicitly rewritten to /2017/slot.php (without any query string paramaters) before your mod_rewrite directives are being processed.
Although it's still not clear where your .htaccess file is located. So, there may still be some work to do... If your .htaccess file is located at example.com/2017/.htaccess then your existing mod_rewrite directives should be sufficient.
However, if your .htaccess file is in the document root (ie. example.com/.htaccess) then you will need to modify your RewriteRule:
RewriteRule ^2017/slot/([a-z-]+)/([a-z-]+)/ 2017/slot.php?gp=$1&sn=$2 [NC,L]
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 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
http://abc.com/test/batch/profile/anyname
In the above url anyname varies depending upon the selected link.
I have to redirect the above url to friend.php and I should pass anyname as parameter to that file
How can I do this?
Put this in your .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^profile/([a-z0-9]+)$ friend.php?username=$1 [NC,L]
This allows that people access the following url on your site:
http://www.yoursite.com/profile/abc
http://www.yoursite.com/profile/abc123
http://www.yoursite.com/profile/123
However, any other characters than alphanumerics will not qualify for the rewrite above.
Edit:
If you actually will be keeping the http://www.yoursite.com/test/batch/-url, the .htaccess would have to be adjusted accordingly. I just assumed that you'd be using the root of http://www.yoursite.com/.
You need to do something like this:
RewriteRule ^/test/batch/profile/([^/?]*)$ friend.php?param=$1
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]