IIRF Rewrite Rule for IIS7 - iis-7

i am using IIRF v2.1 for Rewrite Rule
i write on rule like this but its not working
RewriteRule ^(prod|pag|staf)/([A-Za-z0-9-]+)/?$ $1.php?iid=$2 [QSA,L]
if i use follwoing url
http://localhost/prod/22/new-item
what i need actual URL is
http://localhost/prod.php?iid=22
yes working version is
RewriteRule /^(prod|pag|staf)/([A-Za-z0-9-]+)/?$ /$1.php?iid=$2 [QSA,L]
but problem here is all the style and include files are not included.
Thanks

just write url like this
RewriteRule ^/(prod|pag|staf)/([A-Za-z0-9-]+)/?$ /$1.php?iid=$2 [QSA,L]
and put / before your style sheets and other includes
like
if your old style sheet is include like this
style/style.css
chnage it to
/style/style.css
same for image and links.
hope this will work fine.

Your regex doesn't allow for new-item to follow the final slash.
RewriteRule ^(prod|pag|staf)/([A-Za-z0-9-]+)/?$ $1.php?iid=$2 [QSA,L]
The /?$ sequence that ends the pattern says... a slash (maybe) and then the end of the string. Your URL, however, does not end in a slash. It ends with a slash and the text "new-item".
A regex that captures that would be something like this:
RewriteRule ^(prod|pag|staf)/([A-Za-z0-9-]+)/? $1.php?iid=$2 [QSA,L]
...which says, you don't care what comes after the optional slash, and because you are using QSA, then any query string in the original URL is appended to the outgoing URL.
But that would basically discard the "new-item" portion of the incoming URL request, which I am not sure you want to do.

If you're URLs will always contain the numbers then the slug, this regex will get each part:
^(prod|pag|staf)/([0-9]+)/([A-Za-z0-9-]+)/?

Related

Wordpress, HTACCESS - redirect single page if iPhone or Safari

I know almost nothing so there may be the problem! But I've tried snippets fro 20 different answers to try and get this and no joy. I have a form on one page which will not work on iPhone or Safari. So I have a second form which will. I need to redirect a single page (https://website.co.uk/normal-page/) to another page (/mobilepage/) only if the browser is Safari, or the device an iPhone.
Here's one effort:
RewriteCond %{HTTP_USER_AGENT} "iphone|safari" [NC]
RewriteCond %{REQUEST_URI} ^/normal-page/$ [NC]
RewriteRule (.*) %{HTTP_HOST}/mobilepage/ [NC,R=301]
Which stops any access to the page at all - which was a great improvement on no effect. I'm sure it's simple but I can't get it to work.
Thanks in advance.
I hope this is the right place to post this. Eventually I found the HTACCESS route didn't work. Different OS/Browser combos seemed to arbitrarily ignore or apply the rules when they shouldn't. So I went for Javascript. Less robust, as a user may have it disabled, but I'm not clever enough to find anything better.
Since I found this whole process hard to find answers for I thought I'd post my solution up here - but with the caveat that I'm very much an amateur poking about - there my be better ways and better code.
<script type="text/javascript">
jQuery(document).ready(function(){
var FormSafe = 1;
if (window.navigator.userAgent.indexOf("Windows")!== -1) FormSafe=2;
if (window.navigator.userAgent.indexOf("Chrome")!== -1) FormSafe=3;
if (window.navigator.userAgent.indexOf("OPR")!== -1) FormSafe=4;
if (window.navigator.userAgent.indexOf("Android")!== -1) FormSafe=5;
if (window.navigator.userAgent.indexOf("BlackBerry")!== -1) FormSafe=6;
if (window.navigator.userAgent.indexOf("Firefox")!== -1) FormSafe=7;
if (window.navigator.userAgent.indexOf("iPhone")!== -1) FormSafe=1;
if (window.navigator.userAgent.indexOf("iPad")!== -1) FormSafe=1;
if ( FormSafe === 1 ) {
jQuery("#iphoneform").show(); } else
{ jQuery("#normalform").show(); }
});
</script>
I used 1-7 just to bug test by seeing the results. It could just be ==1 for good, 0 for bad for all others. But 1-7 means you could show different div for each browser.
Then it simply shows or hides the element with a form which works for whichever browser/OS combo.
Hope it helps someone.
This doesn't work for three reasons. First of all, your 2nd RewriteCond will only work with /normal-page/. So if the trailing slash isn't there, it will not pick up the URL. Using /normal-page fixes this and works with and without the trailing slash.
The biggest issue was with your rewriterule. Your were basically trying to rewrite the URL to: http://example.com/example.com/mobilepage/ which is obviously breaking your page.
You also shouldn't need the "" around your first Condition.
Use these rules instead:
RewriteCond %{HTTP_USER_AGENT} iphone|safari [NC]
RewriteCond %{REQUEST_URI} ^/normal-page [NC]
RewriteRule ^(.*)$ /mobilepage/ [L,R=301]
Make sure you clear your cache before testing this. Also, keep in mind that R=301 is a permanent redirect, I advise you change this to R=302 while testing, this is a temporary redirect.

Too many redirects regex redirect

I am on Wordpress and right now using Yoast Seo Pro which has a redirection section including a Regular Expressions redirects section.
How do I redirect say /mycategory1/page/pagenumberhere/ to
/category/mycategory1/page/pagenumberhere/ ?
So I only need to make one redirect that handles all possible page numbers?
I have tried /mycategory1/page/([0-9]) to /category/mycategory1/page/$1
It looks like it redirects to /category/mycategory1/page/pagenumberhere/ but there is an err_too_many_redirects on the /category/mycategory1/page/pagenumberhere/ with this rule added so I have removed it again.
If you can help with code into the .htaccess instead perhaps I could try that.
Your rule already looks promising.
However, as #starkeen pointed out, the regular expression mycategory1/page/([0-9]) matches every request containing mycategory1/page/ with some trailing number. This is true of category/mycategory1/page/ as well, as it also contains "mycategory1/page".
If you want to match requests starting with mycategory1/page, you must anchor the regular expression at the beginning with ^, see Apache mod_rewrite Introduction - Regex vocabulary
RewriteRule ^mycategory1/page/([0-9]) /category/mycategory1/page/$1 [L]

mod RewriteRule to redirect css requests to minify?

I am attempting to redirect all requests of the main stylesheet called styles.css to the minify script of min/g=css so that I may be able to work on the CSS file live and just have the server use redirect to the minify script.
Basically, this in my html file...
<link rel="stylesheet" href="css/styles.css" type="text/css">
turns into this
<link rel="stylesheet" href="min/g=css" type="text/css">
when the server requests it.
Here is my attempt so far, but it doesn't seem to be working at all. The regular css file just loads on the server..
RewriteRule ^css/styles\.css$ min/g=css
Along these same lines, I also have a fear about when I start building subdirectory pages. Does this rewrite rule need to be an "absolute path"? Thanks for any help everyone!
Along these same lines, I also have a fear about when I start building subdirectory pages. Does this rewrite rule need to be an "absolute path"? Thanks for any help everyone!
The rewrite engine strips off the leading slash of the URI when applying rules in an htaccess file, but if the rules are in server/vhost config, then you need the leading slash. The targets (the min/g=css part of your rule) also doesn't need to be an absolute path, but if there is a leading slash, apache will apply that to the base (usually the document root). Without a leading slash, apache tries to guess whether you mean a file-path or a URI-path, sometimes it guesses wrong. If you include a RewriteBase directive, then apache will always use that as its base.
So the question here really becomes: where do you have these rules? In an htaccess file, you're only left with the question of the base. If it's in server/vhost config, you need to know the base as well as add a leading slash to the regex: ^/css/styles\.css$. Or you can be safe and make it optionsl: ^/?css/styles\.css$
Now, the base. The base is where relative paths get appended to. Your css link is this css/styles.css. If that page is loaded from, say, this URL: http://domain.com/some/path/mypage.html, then the base is http://domain.com/some/path. and the resolution of the relative path is http://domain.com/some/path/css/styles.css. In this example, your rules need to go in the directory where /some/path points to and you need a:
RewriteBase /some/path/
above your rule. Otherwise, you can simply append that base to the regex and target of your rule:
RewriteRule ^/?some/path/css/style\.css$ /some/path/min/g=css [L]

Evaluating a external rewrite rule before internal wordpress rewrite rule

I am been plucking out my hairs since last few days trying to solve this problem:
I want to convert my urls from format
http://example.com/prodsearch/category/tag1-tag2-tag3-tag4
to
http://example.com/prodsearch/index.php?tag=tag1+tag2+tag3+tag4&cat=category
to start with I added following rules to my .htaccess
RewriteRule ^prodsearch/(.*)/(.*)-(.*)$ prodsearch/$1/$2+$3 [L]
RewriteCond %{REQUEST_URI} !^prodsearch/(.*?)/(.*)\-(.*)
RewriteRule ^prodsearch/(.*)/(.*)$ index.php?tag=$2&cat=$1 [R,QSA,L]
Here rule1 recursively replaces all '-' between tags by '+'
and rule2 does the actual rewrite once there are no more '-' left between tags (checked by RewriteCond)
These rules actually work, but the problem is they redirect (with url change) to new url pattern, I don't want an explicit redirection. On removing 'R' flag from last rule, the whole thing stops working and I start getting 404 page in my wordpress install.
Can some one explain why this is happening and how to do this without explicit redirection.
Also I tried including these rules into wordpress, hoping when called by wordpress rules may work without redirection. I used following add_rewrite_rule() calls:
add_rewrite_rule('prodsearch/(.*)/(.*)-(.*)$','price-list/$1/$2+$3','top');
add_rewrite_rule('prodsearch/(.*)/(.*)$','index.php?tag=$matches[2]&cat=$matches[1]','top');
Now wordpress detects my first rule as external rule and flushes it to .htaccess file, this screws my execution order, now first rule 2 gets evaluated first(being part of internal wordpress rewrite rule set) and rule 1 gets executed later, hence again 404 page.
Is it possible to tell wordpress not to consider my first rule as a external rule and not to flush it to .htaccess.
Or does anyone has any idea to make this kind of rewrite work? Thanks a lot.

mod_rewrite how to show images in a certain dir

I am starting to use mod_rewrite and would like to know if the below is possible...
RewriteRule ^test/([^/]*)/$ /test.php?x=$1 [NC,L]
That works as expected however, once the HTML generates I use relative paths to images/stylesheets etc such as <img src="include/image.jpg" /> which now no longer shows.
How can I get around this?
Many Thanks
One way to do it is to add a rule to skip requests that end in standard image file suffixes:
RewriteRule \.(gif|jpe?g|png|ico)$ - [NC,S=1]
RewriteRule ^test/([^/]*)/$ /test.php?x=$1 [NC,L]
There are several possibilities:
Don't use relative paths.
Add a <base> tag to your HTML.
Rewrite the image URLs too:
RewriteRule ^test/include/(.*)$ /include/$1 [NC,L]

Resources