htaccess dynamic query string - php

maybe this is a stupid question
i have current code below:
RewriteEngine On
RewriteRule ^(.*) /image/modify.php?pic=$1 [QSA]
and i enter the url http://site.com/image/20121207/test.jpg&w=256
will display the resized image.the original url is:
http://site.com/image/modify.php?pic=20121207/test.jpg&w=256
but how can i change &w= to ?w= ?
the w= are random,perhaps someone will enter h= / r= ...etc
please give me some hint

The Apache rewriter is intelligent enough to process query parameters correctly. That's what the QSA is for, "query string append". You can access 20121207/test.jpg?w=256 and it should be rewritten to modify.php?pic=20121207/test.jpg&w=256 correctly.

Try this:
RewriteRule ^([^/]+)/([^/]+)/([^?&]+)?(.*) /image/modify.php?pic=$1$2$3&$4 [QSA]

So easy :
RewriteEngine On
RewriteRule ^([^/]*)(?)(.*)$ /image/modify.php?pic=$1&$2 [QSA]
Hope it work ....

How about this:
RewriteEngine On
RewriteRule ^image/([^/]*)(?)(.*)$ /image/modify.php?pic=$1&$2 [QSA]
and enter the url:
http://site.com/image/20121207/test.jpg?w=256
i has try it, i think it will work ...
Good luck

Related

How to Make .htaccess search by allow mod_rewrite as query string and page number?

My original file is a PHP page named search.php.
Normally, the search function will be:
mydomain.com/search.php cid=&type=search&q=keyword+text&page=2
However, is it possible to rewrite it to mydomain.com/all.html?q=keyword+text&page=[1-anypage]?
I tried to put this code into .htaccess:
RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]
but something went wrong.
Please help me find the solution. Thanks.
Yes its possible but you can't match against query string in pattern of a RewriteRule, you will need to use RewriteCond directive to check %{QUERY_STRING} something like the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]

URL Rewrite: PHP variable as directory

Alright, how would I get something like:
domain.com/friends.php?id=Fred
Into something like:
domain.com/Fred/friends.php
Previously, I have used:
RewriteRule ^([A-Za-z0-9]+)$ profile.php?id=$1 [L]
To turn
domain.com/profile.php?id=Fred
Into
domain.com/Fred
I know there is a way to do it, I just don't have the knowledge of the operators that would put that extra 'friends.php' in the RewriteRule. Thanks for the help, if my explanation made sense.
You're close:
RewriteRule ^([A-Za-z0-9]+)/friends\.php$ friends.php?id=$1 [L]

mod rewrite, htaccess and gettign the values

the incoming URL
domain.com/12/3
should be re-written to
domain.com/?w=12&h=3
htaccess
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?w=$1&h=$2 [QSA]
the php
<?php
echo $_GET['h'];
?>
Result
404 page
I've tried using htaccess to change the result of the url and then retrieve the value from the URL, could someone help me out?
Maybe you need to escape - like:
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_\-]+)$ index.php?w=$1&h=$2 [QSA]
PS. I hope the above URL /12/3 is just for example because your regex accepts only a-z
RewriteEngine On
RewriteRule ^[a-z]{2,2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Essentially, I'm pretty sure you need a / in front of index.php
I like to use this generator for my mod-rewrite rules.
http://www.generateit.net/mod-rewrite/
Try changing your rewriterule to the following:
RewriteEngine on
RewriteRule ^([a-z0-9]{2})/([a-zA-Z0-9_-]+)$ /index.php?w=$1&h=$2 [QSA]
Your example has two digits as the first parts of the path, which won't be matched by [a-z].
RewriteEngine On
RewriteRule ^(.*)/(.*)$ index.php?w=$1&h=$2 [QSA]
The above sorted it. Thanks for the replies

modify URL using .htaccess

I want to change url for example http://www.mysite.com/cgshop/admin/index.php?route=common/home will become http://www.mysite.com/cgshop/cp/index.php?route=common/home.
Please help me to figure out this. Thanks in advance.
This should get you what you want.
It tests to see if the URI begins with /cgshop/admin/ and then captures the rest so it can redirect with a 301 to the new url. The QSA means it will also carry over the query string (everything after the ?).
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgshop/admin/.*$
RewriteRule ^cgshop/admin/(.*)$ /cgshop/cp/$1 [R=301,L,QSA]
RewriteEngine On
RewriteRule ^cgshop/cp/.*$ cgshop/admin/$1
Try this.......

Rewriting URL rule, example like StackOverflow

What would be the apache rewrite url to extract the id number (434376) from this url?
http://stackoverflow.com/questions/434376/unique-url-slug
something like
RewriteEngine On
RewriteRule ^questions/([0-9]+)/?$([A-Za-z0-9-]+) post.php?post_id=$1 [NC,L]
...but it doesnt work on my site...any tips?
thanks
Tested:
RewriteRule ^questions/([0-9]+)/?.*$ rewrite.php?post_id=$1 [NC,L]
Well if the / is optional it probably should be:
RewriteRule ^questions/([0-9]+)/?([A-Za-z0-9-]+)?/?$ post.php?post_id=$1 [NC,L]
Let me know if it works..

Categories