ISAPI Rewrite rule to allow optional parameters - iis

I am using ISAPI rewrite to rewrite a news feed to our web site. The problem I have is that the rewrite rule does not account for optional parameters that may be passed for google tracking.
A typical link is a follows:
http://www.phmotorcycles.co.uk/motorcycle_news/categories/motorcycle-parts/news_801580922_rossi-jerez-is-%EF%BF%BDvery-important%EF%BF%BD.html
The rewrite rule that covers this is:
Rewriterule ^/motorcycle_news/categories/(.*?)/news_(\d+)_(.*)\.html$ /newsItem.asp?cat=$1&cpID=$2&page=$3 [NC]
Obviously the issue lies in the use of the $ after the .html part of the rewrite rule. The question is how can I change this so that a combination of possible google parameters can be added to the url without causing the link to break on the server?
parameters like:
?utm_source=dlvr.it&utm_medium=twitter
Any clues are greatly welcomed and appreciated.

Please try to fix your rule as follows:
RewriteCond %{QUERY_STRING} ^(utm_source=.*&utm_medium=.*)?$ [NC]
Rewriterule ^/motorcycle_news/categories/([^/]+)/news_(\d+)_(.*)\.html$ /newsItem.asp?cat=$1&cpID=$2&page=$3 [NC]

Related

Redirect and keep the parameter in the url on .htaccess

It should probably be an easy setup, but without familiarity with the terms used, I can't come up with a solution. I tried several examples that I found on the internet.
I just wanted that when the user accessed the site via a url https://example.com?foo=bar he would be directed to https://www.example.com/new-nice-page?foo=bar
Changing the page and keeping the parameter.
The last thing I tested:
RewriteEngine on
RewriteRule ^https://www.example.com/new-nice-page?foo=$ example.com?foo=$1 [QSA]
You may use this redirect rule at the top of .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)foo= [NC]
RewriteRule ^/?$ /new-nice-page [L,R=301]
Note that query string is automatically carried forward to target URL.
Looking at this thread : simple .htaccess redirect : how to redirect with parameters?
you simply redirect , without worying about the parameters, because they are automatically passed :
Redirect permanent /jsn.php http://www.site2.com/jsn.php

mod_rewrite - change top level domain

im trying to find out an issue with the following mod_rewrite Rule:
On my Account on "Strato" (Hoster) the Rule is working, however it is not working on "1und1" (another Hoster). Is there anything wrong with that rule?
# Rewrite Rule
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^.*original-domain.de [NC]
RewriteRule /* http://new-subdomain.anotherdomain.de%{REQUEST_URI} [P]
# End Rewrite Rule
Is there perhaps another or even better way to get the redirect done?
The Goal is that a User who is visiting http://www.original-domain.de (with or without www.) should see the content of http://new-subdomain.anotherdomain.de but the original-domain Adress still stays in the Adressbar of the Browser. In other words, the User won't even recognize that he got redirected.
And, another Question: What stands the [P] for after the Rewrite Rule? If i change it to [L] the redirect works but with the new-subdomain.anotherdomain.de Adress beeing displayed in the Browser? Couldn't find a Website that describes that properly.
Thanks for your help in advance!

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

.htaccess redirect based on url value

I have a url as below.
http://example.com/listing?listingid=237508&2000
What I want is to redirect it to a mobile version of the site.
http://example.com/mobile-listing?listingid=237508&2000
The problem is that if it is example.com/listing it should redirect to example.com/mobile-listing. If it is example.com/event it should redirect to example.com/mobile-event and so on. How am I to do this? I have seen question about redirection to mobile version based on url parameters or getvalue. But not this way. Any help would be greatly appreciated. Thanks.
i've found a simuluar answer from ulrich palha which might helps.
question id 9113747
in the .htaccess file of root you can try this:
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)listingid= [NC]
RewriteRule ^ mobile-listing [L,R=301]
hopes this helps. you might change a little and this should point in the right direction to the solution.
You need to use a RewriteRule directive and QSA and PT flags
<VirtualHost *:80>
RewriteEngine on
RewriteRule ^/listing$ /mobile-listing [PT,QSA]
…
</VirtualHost>
Flags
PT:
Forces the resulting URI to be passed back to the URL mapping engine
for processing of other URI-to-filename translators, such as Alias or
Redirect. details ...
QSA:
Appends any query string from the original request URL to any query
string created in the rewrite target.details
More information
You can find more scenarios at Redirecting and Remapping with mod_rewrite.
Without any sort of condition for what is mobile or what isn't, and you always redirect, then the rule would look something like this:
RewriteCond %{REQUEST_URI} !^/mobile-
RewriteRule ^([^/]+)$ /mobile-$1 [L,R]
The query string will automatically get appended. Change the R to R=301 if you want to permanently redirect.

.htaccess file using 2 different rewrite rules

I've got a .htaccess file that has got a rewrite rule in it as follows which works fine:
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]
What I'm looking to do is to keep using this for if the page variable is 2 or higher, but if it's 1 I want to 301 redirect to a separate url (the same site) say http://www.domain.com/solicitorsinCOUNTY/
The problem is that if I try doing this using a 301 redirect or a rewrite rule it still performs the above rewrite rule as well so I end up with http://www.domain.com/solicitorsinCOUNTY/?county=COUNTY&page=1
I haven't done much with .htaccess before so I'm not even sure if this is possible, can anyone help please? It would be much appreciated.
If you are using a rewrite rule, then put the rule for page=1 above the other rule and make sure you have the [L] flag.
Alternatively, you can use RewriteCond to prevent the rule from being run on specific URLS like this:
RewriteCond %{REQUEST_URI} !^solicitorsin([^/]+)/all/1$
RewriteRule ^solicitorsin([^/]+)/all/([0-9]+)$ /search/searchresults.php?county=$1&page=$2 [L]

Resources