.htaccess questions - .htaccess

Say I have the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image01.gif$ http://www.domain.tld/images/partner/image01.gif [NC,QSA]
RewriteCond %{HTTP_COOKIE} name=value [NC]
RewriteRule ^image02.gif$ http://www.domain.tld/images/partner/image02.gif [NC,QSA]
What do NC and QSA mean?
Also, instead of repeating the same RewriteCond twice is there to use it just once and have it apply to both RewriteRules?
Finally, if the above .htaccess is located at http://www.domain.tld/images/ why doesn't a RewriteRule like this work?:
RewriteRule ^image02.gif$ /images/partner/image02.gif [NC,QSA]
Or maybe this?:
RewriteRule ^image02.gif$ partner/image02.gif [NC,QSA]

The square bracket options are documented in the RewriteRule manual page:
'nocase|NC' (no case):
This makes the Pattern case-insensitive, ignoring difference
between 'A-Z' and 'a-z' when Pattern is matched against the current URL.
'qsappend|QSA' (query string append):
This flag forces the rewrite engine to append a query string part
of the substitution string to the
existing string, instead of replacing
it. Use this when you want to add more
data to the query string via a rewrite
rule.
As far as I know, the RewriteCond directives affect the RewriteRule they precede. If you were setting rules in the main confing file you could write the common directives in a file and include it several times but that's not an option in .htaccess files, sorry.
Your directive works for me, although you probably mean this:
RewriteRule ^image02\.gif$ /images/partner/image02.gif [NC,QSA]
How are you testing it exactly?

NC is for No Case, meaning it can be upper or lower case and it will take you to the same page.
QSA is for query string append. Not really sure on this one, however a quick search http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html sheds a bit more light on this one.

Related

.htaccess with variables for user friendly urls

I got these two urls:
/portfolio/stamped_concrete
/p_details.php?id_cat=23&?id=91
I want to make the second url rewrite to:
/portfolio/stamped_concrete/23/91
stamped_concrete is a dynamic url which is why I'm at a loss of how to solve this. Also the two files (portfolio.php and p_details.php) are in the same directory if that matters.
How would I accomplish this?
EDIT:
stamped_concrete is also a variable string that I rewrote before and it works:
RewriteRule ^services/([a-z0-9_-]+)$ /services.php?url=$1 [NC,L,QSA]
so how would I call it within the RewriteRule?
would this be on the right track?
RewriteCond %{QUERY_STRING} url=([a-z0-9_-]+)
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/$1/$2/$3
Try this:
RewriteCond %{QUERY_STRING} id_cat=([0-9]+)
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule /p_details.php?.* /portfolio/stamped_concrete/$1/$2
Might need to tweak it a bit -- not sure if the RewriteRule part is correct (sorry).
But, the important part is QUERY_STRING, see the apache docs: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also, from http://wiki.apache.org/httpd/RewriteQueryString
I always mix up the backreferences, so try it out, it's eiher dollar signs or percent signs (i really thought it was dollar signs...)
(QUOTE)
Making the Query String Part of the Path
Take a URL of the form http://example.com/path?var=val and transform it into http://example.com/path/var/val. Note that this particular example will work only for a single var=val pair containing only letters, numbers, and the underscore character.
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/path /path/%1/%2?
(END QUOTE)
So you could probably just say "RewriteRule ^/p_details.php /portfolio/%1/%2/%3"

Is it possible to mod_rewrite dynamic url to static url with queries?

Here's what I'm trying to achieve:
Before:
www.example.com/index.php?page=about&pg=5
After:
www.example.com/about.html?pg=5
The URL must specifically have that query at the end. I wanted to avoid using these static methods:
www.example.com/page/about/pg/5/
or
www.example.com/about/5/
Here's what I have so far:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z-]+)\.html\?pg\=([0-9]+)$ index.php?page=$1&pg=$2 [L]
When I var_dump($_GET); only the 'page' parameter was visible.
I wanted the result as if I type in the address bar, www.example.com/about.html?pg=5 would mean the same as typing www.example.com/index.php?page=about&pg=5. Instead of using .php I wanted for it show as .html. the parameter 'pg' is for me to use as hyperlink for various pages. I wanted to strip out the 'index.php' and make it as 'about.html', and the rest of the query follows.
For an example, this page will be used for an article. There will be a section for comments in that article. I wanted to control the pages of the comments with the parameter 'pg'.
If I understand you right, you want to rewrite about.html?... to index.php?page=about&.... If so, there's a simple solution: just use the [QSA] flag.
RewriteRule ^([0-9A-Za-z]+)\.html$ index.php?page=$1 [NS,QSA]
More generally, you can use a RewriteCond to match text in the query string. As the documentation says:
"If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively."
For example, another way to achieve the same effect without the [QSA] flag would be:
RewriteCond %{QUERY_STRING} ^pg=([0-9A-Za-z]+)$
RewriteRule ^([0-9A-Za-z]+)\.html$ index.php?page=$1&pg=%1 [NS]
Or, if you want to go the other way (as I first read your question):
RewriteCond %{QUERY_STRING} ^page=([0-9A-Za-z]+)&pg=([0-9]+)$
RewriteRule ^index\.php$ %1.html?pg=%2 [NS]
(Edit: Removed misplaced question marks from RewriteConds per comments.)

.htaccess mod-rewrite conditional statements and sending GET request?

Not necessarily a problem, just something that I am not yet knowledgeable enough to do. I have an .htaccess file that I am using for url rewriting. This is what I have now.
ErrorDocument 404 /inc/error_documents/404.php
ErrorDocument 503 /inc/error_documents/503.php
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine ON
RewriteRule ^home$ /index.php [nc]
RewriteRule ^(about|contact|giving-tree)/?$ /$1.php [nc]
RewriteRule ^giving-tree/([0-9+]?)/?$ giving-tree.php?ageBegin=$1 [nc]
RewriteRule ^giving-tree/([0-9+]?)/([0-9+]?)/?$ giving-tree.php?ageBegin=$1&ageEnd=$2 [nc]
RewriteRule ^giving-tree/([0-9+]?)/([0-9+]?)/([0-9+]?)/?$ giving-tree.php?ageBegin=$1&ageEnd=$2&page=$3 [nc]
What I want to be able to do is make some of the parts in the 3 bottom rules optional. I know that I can accomplish this with RewriteCond, but I'm not sure how. What I need is basically this:
RewriteCond %{HTTP_HOST} ^hearttohandparadise.org/giving-tree
RewriteRule /beginAge-([0-9+]) #make it send GET request with beginAge as the variable
RewriteRule /endAge-([0-9+]) \?beginAge=$1 #make it send GET request with endAge as the variable
etc... etc...
Is there any way to accomplish this just by relying on .htaccess? or am I just fantasizing?
Forgive me is I sound stupid.
No, it's a perfectly valid idea. You'd basically want to allow the user to write the URI in an unstructured manner, without a strict order imposed, right? Like, I could write giving-tree/page-6/endAge-23?
If so, this is what you're looking for:
RewriteRule /beginAge-([0-9]+) giving-tree.php?beginAge=$1 [QSA,NC]
RewriteRule /endAge-([0-9]+) giving-tree.php?endAge=$1 [NC,QSA]
RewriteRule /page-([0-9]+) giving-tree.php?page=$1 [NC,QSA]
You see, if any part of the URI matches the expression "/beginAge-([0-9]+)", it'll be redirected to giving-tree.php?beginAge=$1; the magic is done by the QSA, Query String Append, option, which, well, appends any existing query string to the resulting URI. So as more and more matches are found and more and more GET parameters added, the query string just grows.
If you want a stricter thing, where some parameters are optional, but their order is fixed, then it's uglier by magnitudes:
RewriteRule /(beginAge-)?([0-9]+)/?(endAge-)?([0-9]+)?/?(page-)?([0-9]+)? giving-tree.php?beginAge=$2&endAge=$4&page=$6 [NC]
I just made everything optional by using the ? operator. This one may use some prettifying/restructuring.
(Alternatively, you could just do this:
RewriteRule ^giving-tree/([^/]+)/?$ process.php?params=$1 [nc]
That is, grabbing the entire part of the URI after the giving-tree part, lumping the whole thing into a single parameter, then processing the thing with PHP (as it's somewhat better equipped to string manipulation). But the first version is certainly more elegant.)
By the way, are you sure about the ([0-9+]?) parts? This means "One or no single character, which may be a digit or the plus sign". I think you meant ([0-9]+), i.e. "one or more digit".

htaccess: Mediafire.com like urls

I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]

Rewrite htaccess old oscommerce links

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

Resources