I have user.php file and I m passing two parameters with it using URL Rewrite
but I m unable to use this rule as whichever rule is written first only gets executed and not the second one in case of my rule for userID rule doesn't work but work for webName. Any solution will be very much helpful to me.Thank you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(.*)$ user.php?webName=$1 [L,QSA]
RewriteRule ^user/(.*)$ user.php?userID=$1 [L,QSA]
Make a difference between two regural expression:
If you would like to catch user ID you should filter for only numbers: \d
Then if there is another chars in url not only numbers you can match for anything except kslash: [^/]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\d+)$ user.php?userID=$1 [L,QSA]
RewriteRule ^user/([^/]+)$ user.php?webName=$1 [L,QSA]
Related
I want to achieve the following
Any links like mydomain.com/anyword should be rewritten to index.php?pg=$1
example:
mydomain.com/new -->index.php?pg=new
mydomain.com/purchased -->index.php?pg=purchased
mydomain.com/login -->index.php?pg=login
Any links like mydomain.com/anyword/anynumber should be rewritten to index.php?pg=$1&id=$2
example:
mydomain.com/new/12 -->index.php?pg=new&id=12
mydomain.com/purchased/240 -->index.php?pg=purchased&id=240
mydomain.com/login/10 -->index.php?pg=login&id=10
I have used the following htaccess code.
The first one works properly.
Regarding the second one, the entire parameter is passed to pg and there is no parameter id at all.
That is in case of url
mydomain.com/purchased/240
in my php $_Get has only 'pg' parameter and 'id' parameter at all
and pg=/purchased/240
Please suggest the proper way to do it.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
RewriteRule ^([^/]+)/(\d+)/.*$ /index.php?pg=$1&id=$2 [NC,QSA]
Thanks
EDIT: Correct solution in case anyone made need it
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+) /index.php?pg=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
This is because of the catch-all pattern. (.*) matches everything and rewrites it to the target. For your example, your first rule matches both requests, /foobar/ and /foobar/123 are rewritten to /index.php?pg=request . To fix this, you need to fix the order of your rules, you need to put your specific rule before the catch-all rule :
RewriteRule ^([^/]+)/(\d+)/?$ /index.php?pg=$1&id=$2 [L]
RewriteRule ((?!index.php).+) /index.php?pg=$1 [L]
I'm no regex expert and I'm trying to implement on a website a social login system that may or may not redirect the user to the page where he was before loggin in.
Like so, for no redirection:
RewriteRule ^login/([^/]+)$ login.php?p=$1 [L]
http://example.com/login/Facebook >> the page does it stuff and goes to index (default behaviour).
or like this for redirection (p = provider; r= redirect relative path):
RewriteRule ^login/([^/]+)/([^/]+)$ login.php?p=$1&r=$2 [L]
Works great if the user is on a page like http://example.com/info that is generated by the following rule:
RewriteRule ^info$ users_infosystem.php [L]
I'm having problem when the user is on a page like http://example.com/gallery/galleryname
RewriteRule ^gallery/([^/]+)$ galery.php?name=$1 [L]
Any Help?
EDIT: Just occured me ... I don't know if the $ char is the delimeter that marks the end and taking it off makes the rule accept everythings that is after the ([^/]+) bit.
Also: Doesn't the ([^/]+) bit match everything but a forward slash?
I'm having problem when the user is on a page like
That is a very vague sentence. You don't actually say what the problem is. What about your rule is not working? Also didn't paste your entire htaccess file, you pasted bits and pieces so we don't even know what order your rules are in or if there are other rules.
I don't see an issue with these rules. These rules should work. They all match something different. Also you can make the / optional using the ? after it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/([^/]+)/?$ login.php?p=$1&r=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/([^/]+)/?$ login.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^info/?$ users_infosystem.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/([^/]+)/?$ galery.php?name=$1 [L]
The last rule will match http://example.com/gallery/galleryname with or without a / at the end.
I have the two following URL like below
www.site.com/index.php?key=blah
www.site.com/index.php?key=blah&pass=blahblah
I want to make it like the following using .htaccess
www.site.com/blah/blahblah
www.site.com/blah
in same webpage
How can i achieve this using .htaccess?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?key=$1&pass=$2
should do it.
Edit: If you want the parameters optional, use this one.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?key=$1
RewriteRule ^([^/]+)/(([^/]+)?)$ /index.php?key=$1&pass=$2
I simply added a new rule where only one parameter is required. It does match first when only one parameter is given. If two parameters a given the second rule matches only.
I have this url myurl.com/index.php?hello=folder/7f6c06
I want to change it to myurl.com/folder/7f6c06
I tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?hello=$1 [QSA,L]
is not working in godaddy hosting.
It doesn't work because your pattern ([^/]+) captures everything up to but not including the first /, but the URI you are looking to capture and pass into hello= includes a / itself. Since there are additional characters after the first / but the pattern matches at most one / optionally at the end, the pattern would never match as you have it.
Instead just use (.+)/? to capture everything up to an optional possible trailing slash.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ index.php?hello=$1 [QSA,L]
I am currently using .htaccess to rewrite my urls from
example.com/mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)
The code i use to accomplish that is below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/?$ mixtape.php?mixid=$1 [L]
The next thing I am trying to achieve, in a clean matter, is to rewrite my
example.com/edit-mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)/edit
EDIT: the "WHATEVER #" is the ID of the Mixtape I am editing. Normally i will use "$_GET['id']" to see what mixtape i'm referring to and then i fetch everything related to that number.
IS there anyone out there that would be able to help me successfully write a proper re-write mod?
Based on your comment, I would change your original rule as well to allow only for numbers:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/?$ mixtape.php?mixid=$1 [L]
By the way, I would probably use a general rule and write both urls to the same php file and handle the action in the controller.
You can use the same rules that you had before but just append an edit to the regular expression:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
Note that your original rules have the query string mixid= while your examples say id=.