Htaccess Rewrite Rule Conditional - php

I am a complete noob when it comes to the .htaccess file... What I'm trying to do is check to see if a GET variable exists, ?si=10, and pass that onto the rewrite, but only if it exists.
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1
This way when I have say website.com/user/Username/ it goes to, on the server, website.com/profile/?name=Username instead. But when I add do website.com/user/Account/?si=10, the server isn't passing the si variable onto the actually loaded page. Any idea how I would do this? Sorry if I worded this badly..

Try the QSA flag
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1 [QSA]
From the manual...
When the replacement URI contains a
query string, the default behavior of
RewriteRule is to discard the existing
query string, and replace it with the
newly generated one. Using the [QSA]
flag causes the query strings to be
combined.

You can do it like this:
RewriteCond %{QUERY_STRING} ^si=([0-9]+)$ // replace regex as neccesary
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1&si=%1
The percentage sign % brings in the match or matches from the rewriteCond(s) and you use the dollar sign as normal for the matches from the rewriteRule. This gives you full control of your query vars, but yes, QSA is simpler.

Related

mod_rewrite and keep query string

I want to do a mod_rewrite URL transformation. I want to direct
http://localhost/events?user=XXX&start=YYY&total=ZZZ
to
http://localhost/?operation=events&user=XXX&start=YYY&total=ZZZ
What I am using currently in my .htaccess is:
RewriteRule
^events\?userid=([0-9]+)&start=([0-9]+)&total=([0-9]+)$
?operation=events&user=$1&start=$2&total=$3
[L]
but it does not seem to be working.
I have also tried a simpler version:
RewriteRule ^events\?([a-zA-Z0-9&=]+)$ ?operation=events&$1 [L]
But it is also not working.
¿How can I "transfer" either the query params one by one OR else the whole query string to the end of the directed path?
How can I "transfer" either the query params one by one OR else the whole query string to the end of the directed path?
This is done using QSA flag. QSA (Query String Append) flag preserves existing query parameters while adding a new one.
However remember that you cannot match query string using RewriteRule directive. You can only match Request URI using RewriteRule.
You can use this rule:
RewriteEngine On
RewriteRule ^/?events/?$ ?operation=userevents [L,NC,QSA]
This will route http://localhost/events URI to http://localhost/?operation=userevents by appending all the original query parameters to rewritten URI. For example: http://localhost/userevents?user=XXX&start=YYY&total=ZZZ will be rewritten to http://localhost/?operation=events&user=XXX&start=YYY&total=ZZZ

mod_rewrite code check double rewrite rule

Here is my current config:
1.RewriteRule ^india/news.php /portal/india/stories/telugu.php??topicid=14
2.RewriteRule ^india/telugu.php?currentpage=([0-9]+).&topicid=14 /portal/india/stories/telugu.php?currentpage=$1&topicid=14
Rule 1 rewrites http://www.domain.com/portal/india/stories/telugu.php?topicid=14 to
http://www.domain.com/india/news.php which works fine.
Rule 2 should rewrite http://www.domain.com/portal/india/stories/telugu.php?currentpage=p.no&topicid=14 to http://www.domain.com/india/telugu.php?currentpage=p.no&topicid=14 which doesn't work.
It shows a 404 not found.
Here p.no = numeric number
If I need to elaborate my problem, please let me know.
RewriteRule uses regular expressions so having a ? in the regular expressions is not denoting a query string but instead kicking off as a regular expressions key.
Also RewriteRule really only looks at the request URI and ignores the query string unless you set a conditional for the query string.
so Rule 2 should be something like this
RewriteCond %{QUERY_STRING} currentpage=([0-9]+).&topicid=14
RewriteRule ^india/telugu.php /portal/india/stories/telugu.php?currentpage=%1&topicid=14
Please note that in this setup you need %1 instead of $1
I hope this helps I have had this issue several times and this is the only way I ever get it to work. Also you can test rewrites before you do them on sites like http://martinmelin.se/rewrite-rule-tester/

URL Rewrite with htaccess and PHP

I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.

Trouble with URL rewriting in .htaccess

My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1

.htaccess with products (string)

i use .htaccess and i have a rule that allow me to dynamically look for product in my database.
so someone can click on a link like this one:
www.domain.com/product/modular-plastic-lunch-set.html
and see the product. Now my problem is, when i use
www.domain.com/product/Modular-Plastic-Lunch-Set.html
it does not work,
why?
here's my rules:
RewriteEngine On
RewriteRule ^product/([a-z0-9\-]+).html$ products.php?name=$1
It does not work because you don't have either A-Z or the [NC] flag.
Use of the [NC] flag causes the RewriteRule to be matched in a
case-insensitive manner. That is, it doesn't care whether letters
appear as upper-case or lower-case in the matched URI.
RewriteRule ^product/([a-z0-9\-]+).html$ php.php?name=$1 [NC,L,QSA]
or
RewriteRule ^product/([a-zA-Z0-9\-]+).html$ php.php?name=$1 [L,QSA]
I added the L:
The [L] flag causes mod_rewrite to stop processing the rule set. In
most contexts, this means that if the rule matches, no further rules
will be processed. This corresponds to the last command in Perl, or
the break command in C. Use this flag to indicate that the current
rule should be applied immediately without considering further rules.
and QSA flag:
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the query
strings to be combined.
More information about the flags at: http://httpd.apache.org/docs/2.3/rewrite/flags.html
TIP: if you are looking for products using the name, you might see delay in your query, speically if you don't have an index. You should look into this before it gets ugly.
You're only looking for lower case letters (and numbers). You need to add upper case letters.
RewriteEngine On
RewriteRule ^product/([A-Za-z0-9\-]+).html$ products.php?name=$1

Categories