I think this has been discussed over here in past but i don't know exactly what i have to search for so please if you could help just give me a hand.
Well i want to create a url rewrite with multiple options. I cannot explain here with few so i would rather give a simple example.
A url:
example.com/info.php?id=1
example.com/info.php?id=1&edit
I want to rewrite to like this
example.com/info/1
example.com/info/1/edit
I think you want the opposite: example.com/info/1 -> example.com/info.php?id=1
RewriteEngine on
RewriteRule ^info/([^/]+)/edit/?$ info.php?id=$1&edit [L]
RewriteRule ^info/([^/]+)/?$ info.php?id=$1 [L]
Related
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 trying to do some URL rewriting with Apache and PHP. This is for SEO reasons so that foo.com/product-category/product-title becomes foo.com/product.php?id=999. I would of course want the URL not to change and be the SEO friendly version.
I have a current site which has a working setup but it's very much a hack rather than a maintainable solution, so I am trying to do things right!
At the moment I have:-
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Which works perfectly, http://foo.com/random-url?foo=1&bar=2 ends up at /url_redirect.php with $_GET['page'] = 'random-url' and $_GET['foo'] = 1 etc etc..
I want the exactly the same thing to happen if you one level deeper
(eg http://foo.com/non-existing-directory/random-url?foo=1&bar=2)
I could make the non-existing-directoy a real directory and then .htaccess from there but there has to be a easer way.. I would prefer me not to have to hardcode any directory names (which is sort of how my current solution works).
I think the issue is probably something not right with the regular expression, they always defeat me.
I just tried this, might not be the best way to achieve it though (I'm no rewrite guru)
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) url_redirect.php?page=$1 [QSA,L]
Sorry that was for my local setup here's how I think you would want it!
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/([a-zA-Z0-9_\ \-\%]+)?($) /url_redirect.php?page=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9_\ \-\%]+)/?($) /url_redirect.php?page=$1 [QSA,L]
Notice the / before url_redirect.php
So I want to use mod_rewrite to transform:
random_name.com/main.php?user=$user
into
random_name.com/$user
(the $user is a php variable added onto the url, so it can be any name such as andy or rebecca, names like that)
and my code for the .htaccess is:
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/?$ main.php?id=$1 [NC,L]
But this doesn't seem to work for some reason. I've read up on the tutorials but they're really complicated and it seems like this would do the trick, but it doesn't. I'll appreciate it if anyone who has experience with mod_rewrite would give me a few pointers.
You cannot use mod rewrite to transform random_name.com/main.php?user=$user into random_name.com/$user.
You have to do it manually in all the links on your site.
After that you may use mod rewrite for the reverse transformation, which will make /main.php?user=$user out of /user request
Try this:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)$ main.php?user=$1 [NC,L]
i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.
Hello I want to have some redirection made by .htaccess file:
Some examples
mysite.com/accepted -> mysite.com/index.php?type=accepted
mysite.com/waiting -> mysite.com/index.php?type=waiting
mysite.com/cancelled -> mysite.com/index.php?type=cancelled
&
mysite.com/edit/2 - > mysite.com/admin.php?edit=2
ETC. for all numbers possible
mysite.com/login -> mysite.com/admin.php?action=login
mysite.com/register -> mysite.com/admin.php?action=register
mysite.com/lostpassword - > mysite.com/admin.php?action=lostpassword
mysite.com/add - > mysite.com/add.php
Apprectiate your help ;)
#edit: It should be done that way but with masking urls.
Which way around is the redirect? It would be common to see redirects from your given right-hand-sides to the left-hand-sides, but not the other way around.
EDIT I see you've fixed that, so my supposition was correct:
i.e. you'd normally see:
RewriteRule ^edit/([0-9]+)$ /admin.php?edit=$1 [L]
etc, to map the nice friendly RESTful style URL into the internal URL.
If you want to get into the topic yourself, www.modrewrite.com hosts a number of great mod_rewrite resources with lots of examples.
If I understand your edit correctly you want this
<?php
header('Location: http://mysite.com/'.$_GET['type']);
?>
Since there is no structural pattern in your URLs that can be used to map the URLs to the destinations, you probably will need to use one rule for each group like these:
RewriteRule ^add$ add.php [L]
RewriteRule ^(login|register|lostpassword)$ admin.php?action=$1 [L]
RewriteRule ^edit/(\d+)$ admin.php?edit=$1 [L]
RewriteRule ^(accept|waiting|canceled)$ index.php?type=$1 [L]
Maybe something like the following?
RewriteRule ^www.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+) /index.php?$1=$2&$3=$4 [NC]
Do note that you will have to parse the URL (REQUEST_URI) instead of accessing $_GET