i try use RewriteRule in htaccess
i want my url
site.com/f_search.php?langs=en&langs=en
works like
site.com/en/new.php?/en/f_search
i used this code in htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php+?+/([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2
but its not work with " ? "
if i change new.php+?+ to new.php+#+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php+#+/([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2
it works good
site.com/en/new.php#/en/f_search
any idea to change # to ? in my url ???
thanks
The question mark and what follows is not part of the subject the pattern of a RewriteRule is matched against. That is clearly documented. Instead you need to use a RewriteCond to access the content of the query string.
I assume this is roughly what you are looking for, though you might have to tweak it to match your actual situation:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^/([a-z]+)/f_search$
RewriteRule ^/?(?:([a-z]+)/)?new\.php$ /f_search.php?langs=$1&langs=%1
From your question it is unclear where the two values for the argument "langs" in the rewrite target should get taken. So you may have to adjust that to your needs in the rule.
Also it is puzzling that you are trying to set that argument "langs" twice ... That makes little sense, the second attempt will simply overwrite the value of the first attempt when those values are handed over to your php based logic.
The documentation of the rewriting module for the apache http server offer a lot of precise help and good examples: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
the final code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z]+)/new.php$ f_search.php?langs=$1 [L,NC]
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 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=.
I want to redirect from /gallery/X to gallery.php?category=X
But when I actually goto the address, my old $_GET variable 'category' is transformed into the form:
$_GET['category'] = "X.php/X"
This is what my .htaccess looks like:
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/(.*)$ /gallery.php?category=$1 [L]
I am very bad at RegEx and no almost nothing about .htaccess. I have been trying to play around with this rewrite rule to preserve the $_GET variables, but nothing I do seem to work. What am I supposed to do here?
Thanks!
Move your rules around:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/(.*)$ /gallery.php?category=$1 [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Also, add QSA to your action, [L,QSA]. This appends the query string during the redirect, so it should keep your previous data.
Explanation:
Your rewrites need to be in a specific order because the [L] option is like making a completely new request to the server. So, when your request for gallery/X came in, it rewrote your request to /gallery.php?category=X. When this page was requested by the server, it matched your first rule, which means it was being seen as X.php/X which was then being returned to the original request as the extra $1.
Sounds confusing but I think that's what was going on.