.htaccess with variables for user friendly urls - .htaccess

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"

Related

Replacing - sign with + Query string in htaccess

I have tried my best but ony little of it works, I want to replace - to + sing withing a specific query string in a category.
i want to replace
https://www.example.com/stack/?s=over-flow
to
https://www.example.com/stack/?s=over+flow
I have tried my best with the below code
RewriteCond %{QUERY_STRING} ^(.+)-(.+)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]
It works but it breaks other part of my site containing - sign in the url
I want it specifically on /stack/?s= only
Please help me out, thanks in advance
You may use this rule to target a specific query string with only single parameter:
RewriteCond %{QUERY_STRING} ^(s=[^&-]+)-([^&]+)$ [NC]
RewriteRule ^(stack/?)$ /$1?%1+%2 [L,R=301,NE]

remove second question mark from query string

I made a mistake in creating links. I corrected it but now still there are links floating around that might look like this:
http://www.domain.com/?page=1?date=29062015&id=778
I would like to correct this using the rewriteEngine to redirect my users to:
http://www.domain.com/?page=1&date=29062015&id=778
I searched around and tried the following, but it doesn't work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule ^\/ ^\/$1\&$2 [L,R=301]
What should I change here?
I've slightly corrected your rule,hope it will help to resolve the issue:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)\?(.*)$
RewriteRule (.*) $1?%1&%2 [L,R=301]
Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond (Query string condition).
Good article about rewriting query string here: https://wiki.apache.org/httpd/RewriteQueryString

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.

Fixing Rewrite Rules and Conditions In HTACCESS file

Well lets say I have this follow code in my htaccess file,
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php
RewriteRule ^forums/([0-9]+) forums.php?category=$1 [NC]
I was wondering how would I, with the above code, redirect certain extensions in a url to my websites 404 page.
For instance, if this link mywebsite.com/forums has any extension at the end of it such as .asp, .php, .html, and so forth it then would get redirected to my 404 page.
And on a quick side note how can I limit the last RewriteRule to only a certain forward slash where mywebsite.com/forums/2 would show the page fine and anything after that certain limit such as mywebsite.com/forums/2/so on... would be redirected to my 404 page.
Anyone have any ideas?
If I understand the question properly, then you need to firm up the regular expressions to only match the patterns you really want - at the moment, they're a bit too lenient for your needs.
For example:
RewriteRule ^([^/]+)$ $1.php
This will match anything without a trailing slash, whereas if you wanted to restrict it to only match, say, things without a trailing slash and consisting of alphanumeric characters, then you might do this:
RewriteRule ^([a-zA-Z0-9]+)$ $1.php
(You could achieve the same effect for certain extensions only by using a lookahead assertion, but that complicates your regular expression. I feel it's probably saner (and easier on the mind) to think about the patterns you really want matched, and then express those up-front.)
Likewise, your latter example:
RewriteRule ^forums/([0-9]+) forums.php?category=$1 [NC]
will match anything which starts with the string forums/, followed by one or more digits, whether or not there's anything after that. Adding an end anchor ($) as you have above
RewriteRule ^forums/([0-9]+)$ ...
will assert that the string ends after the digits.
This relies on the fact that if mod_rewrite can't find a match, it won't attempt any rewrites, and will (in the absence of any explicit resource at that path) fall through to Apache's 404 handling, which is then up to you to override.

.htaccess questions

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.

Resources