Not matching correct parameter - .htaccess

I'm trying to use mod_rewrite to overwrite a subdomain to a set of query parameters.
For example, sub.domain.com should direct to index.php?option=com_pages&layout=swf, while sub.domain.com/about should direct to index.php?option=com_pages&layout=swf/about.
I currently have this:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteRule ^(.*)$ index.php?option=com_pages&layout=swf/$1 [L,NC,QSA]
It works correctly for sub.domain.com, but for sub.domain.com/about, I'm directed to index.php?option=com_pages&layout=swf/index.php and I'm not sure why.
I've ready a few questions on here, a few tutorials and still can't seem to see what I'm seeing. Any assistance is appreciated.
I'm on Apache 2.2 if that makes a difference.
EDIT: I've taken a look at my server and I'm getting the following for $_SERVER:
[REDIRECT_QUERY_STRING] => option=com_pages&layout=swf/about
[QUERY_STRING] => option=com_pages&layout=swf/index.php&option=com_pages&layout=swf/about
I'm not sure why its being concatenated like that.

I was using the QSA-flag which appends the query string.

Related

I am not able 301 redirect domain.tld/?cur=usd to domain.tld

I try to redirect domain.tld/?cur=usd to domain.tld (there are many curencies, this is only example of one currency - we do not use anymore this solution).
I need to redirect only home with parameter to home without parameter. The other urls worked for me, I'm just having trouble getting work with that one.
I try to search and use online generators but none of the solutions work.
Here is what I am trying:
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
// update
before this rule I have only
#bof redirects
RewriteEngine enabled
...and then there are redirects for other URLs, but I tested this rule separately first and the result was the same...
It not redirect me.
Thanks for the help and maybe an explanation of what I'm doing wrong.
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
As mentioned in comments, this should already do as you require, providing there are no conflicts with other directives in the .htaccess file.
However, the regex in the preceding condition is excessively verbose for what you are trying to achieve (ie. just testing for the presence of the cur URL parameter).
If you simply want to check for the cur URL parameter anywhere in the query string then the regex (^|&)cur= would suffice (and is more efficient). No need to backslash-escape the literal =. And if the URL parameter always appears at the start of the query string then just use ^cur=.
I found the problem - it was something with the hosting, after a reboot everything started working as expected.
So I can confirm that this rule is fine.
Sorry for question.

htaccess rewrite to capture filepath and query string separately

I'm trying to get 2 different parts of a url and use them in a rewrite but I can't get it to work
I'd like
http://example.com/account/test-page?h=1&t=2
http://example.com/account/test-page
to rewrite to
http://example.com/page.php?path=account/test-page&h=1&t=2
http://example.com/page.php?path=account/test-page
I've tried a dozen different ways - this is the latest one :
RewriteRule ^http:\/\/example.com\/([^\?.]*)[\?]?([^/]*)$ http://example.com/page\.php?url=$1&$2
but it doesn't work and I'm tearing my hair out !!
could someone tell me where I'm messing up please ?
Please use the following rule instead:
RewriteRule ^(.*)$ /page.php?path=$1 [QSA,L]
The key, here, is the QSA flag, which appends in query string used to the query string already passed to page.php.
To be clear, a request made to /account/test-page?h=1&t=2 will be internally rewritten as /page.php?path=account/test-page&h=1&t=2.

Rewrite url to remove gclid query

I am trying to remove google's gclid tracking parameter from my urls. After searching around the internet it appears that I need to use url rewriting to solve the problem.
I am using IIS6 as a server so rather than .htaccess I am using isapi rewrite filter which is supposed to work the same way.
Ideally I would like to make this:
http://www.example.com/default.asp?parameter=stufftokeep&gclid=alotofrandomstuff
Become this:
http://www.example.com/default.asp?parameter=stufftokeep
no matter what comes after "gclid"
I was able to find this here on SO but replacing "tag" with "gclid" did not work, and keeps breaking the page.
Has anyone run into this issue with gclid parameter before who might be able to offer a solution?
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)gclid=[^&]+(&.*)?$
RewriteRule ^(.*)$ /$1?%1%2 [L,R=301,NE]
In case anyone else has this issue with gclid parameter on IIS6 server using isapi rewrite filter v2 here is the code that solved the issue.
RewriteRule ^/([^?]+)\?(.*)(?:^|&)gclid\=[^&]+(\&.*)?$ /$1\?$2$3 [I,RP,L]

using mod_rewrite to strip out junk

We're seeing some really weird URLs in our logs and I've been told to start redirecting them.
I know of a couple of better ways to go about fixing this, but the boss wants it done this way. I apologize in advance.
We're seeing stuff like the following in our logs:
http://www.example.com/foo/bar/bla&ob=&ppg=&rpp=100&ob=&rpp=&ppg=&rpp=30&ppg=&ppg=1&rpp=10&rpp=50&ob=&ob=&ob=&rpp=40&ob=&rpp=5&rpp=30&rpp=&rpp=20&order_by=&results_per_pge=75
I've been told to 'toss some mod_rewrite rules in the .htaccess file' to take this and strip out all the ob, rpp, and ppg variables.
Now, I've found ways to strip everything out. And that wouldn't be too bad if I could leave the /foo/bar/bla in there. But I can't seem to do that. Basically, any help would be appreciated.
Try:
# strip out any params that's ob=, rpp= or ppg=
RewriteRule ^/?(.*)&ob=([^&]*)&(.*)$ /$1&$3 [L]
RewriteRule ^/?(.*)&rpp=([^&]*)&(.*)$ /$1&$3 [L]
RewriteRule ^/?(.*)&ppg=([^&]*)&(.*)$ /$1&$3 [L]
# if everything's gone, finally redirect and fix query string
RewriteCond %{REQUEST_URI} !&(ob|rpp|ppg)
RewriteRule ^/?(.*?)&(.*) /$1?$2 [L,R=301]
The problem here is that your URL:
http://www.example.com/foo/bar/bla&ob=&ppg=&rpp=100&ob=&rpp=&ppg=&rpp=30&ppg=&ppg=1&rpp=10&rpp=50&ob=&ob=&ob=&rpp=40&ob=&rpp=5&rpp=30&rpp=&rpp=20&order_by=&results_per_pge=75
has A LOT of ob=, rpp=, and ppg= in the URI. More than 10. That means you'll get a 500 internal server error if you use these rules against that URL. By default, apache has the internal recursion limit set to 10, that means if it needs to loop more than 10 times (and it will for the above URL), it'll bail and return a 500. You need to set that higher:
LimitInternalRecursion 30
or some other sane number. Unfortunately, you can't use that directive in an htaccess file, you'll need to go into server or vhost config and set it.

Subdomain redirect to URL Parameter while keeping other url params intact

I've looked like crazy for an answer to this - and I haven't been able to find one as of yet. My apologies in advance if I've missed something.
I'm looking to convert a subdomain to a url parameter while maintaining the other url pieces.
Such as:
http://sub.domain.tv/value redirects to http://domain.tv/value?campus=sub
This is what I've tried after looking through numerous posts on here:
# campus
RewriteCond %{HTTP_HOST} ^sub.domain.tv$ [NC]
RewriteRule ^(.*)$ http://domain.tv/$1/?campus=sub [R=301,L]
which worked fine for redirecting sub.domain.tv to domain.tv?campus=sub, but does not work with the rest of the URL parameters (so heading to sub.domain.tv/value does nothing).
I've tried other examples with the %1 and $1 being used as variables, such as with this post:
htaccess subdomain redirct with last url parameter but I don't understand the difference (and the examples I've tried did not work).
Thanks for taking a look! :) I very much appreciate it!
Have you tried the [QSA] parameter. This will add any remaining Query parameters to campus=sub

Resources