Pattern Matching htaccess conflict - .htaccess

I have migrated my site to new software and am trying to ensure that older links are appropriately redirected to the new url's. I thought I had it working until I received the message from Google regarding increasing 404's.
I seem though to be causing a conflict between the new htaccess requirements and my changes to address the old links.
So the new links look like this:
http://www.exampledomain.com/search/?q=searchterm
And in the htaccess they are picked up like this:
RewriteRule ^search/(.*)$ search.php?q=$1
The above is working as it should.
The old links can look like either of these:
http://www.exampledomain.com/search/searchterm/
or
http://www.exampledomain.com/search/searchterm
I had put this in to the htaccess
RewriteRule ^search/(.*)/$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
If I don't add the first rule then the new url's just bring up 404's.
If I add second rule the it stops the searchterm being passed and conflicts with the first rule.
Have tried a few things but think there must be an issue with the matching or something else i'm missing.
Any ideas appreciated.
** Added **
So after the first reply I made the change as suggested and it caused a couple of issues but switching the order of the rules has fixed that but has not quite fixed the issue
So now I have this:
RewriteRule ^search/(.*)/?$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
RewriteRule ^search/(.*)$ search.php?q=$1
The above works for these url's now:
http://www.exampledomain.com/search/?q=searchterm
and
http://www.exampledomain.com/search/searchterm
But for url's like this:
http://www.exampledomain.com/search/searchterm/
it results in this with a trailing slash which prevents the search:
http://www.exampledomain.com/search.php?q=drama/
So just need to remove or not have the trailing slash

I think you are just missing a ? in the rule since the / is optional:
RewriteRule ^search/(.*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address trailing slash:
I'm guessing that the .* is consuming the trailing / before the next rule. To fix that, we need to exclude it from the match:
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address added case where parameter appears after slash:
I'm not sure if you mean literally /search/q=searchstr or /search/?q=searchstr so I will attempt to address both cases.
If it's the latter, which is a true query string, place this rule above the first using QSA in order to pass the query string along to the new URL:
RewriteRule ^search/$ search.php [R=301,NC,QSA,L]
To address the first variation (without the ? query string), you will need to place this rule above the first, which literally looks for the q=:
RewriteRule ^search/q=(.*)$ search.php?q=$1 [R=301,NC,L]
And since I get the feeling you'll update this question again to ask about what happens if there is a trailing slash, I'll go ahead and modify that rule to handle this case as well:
RewriteRule ^search/q=([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
If these rules still don't solve every case for you, then you're dealing with some really bad code from your previous URL and I feel very sorry for you. :P

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.

Htaccess - Detecting the URL

For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.

erase part of a URL using htaccess rewrite

i need to remove part of Joomla/Virtuemart generated SEF URI using .htaccess
the URI represents a menu hierarchy and structured this way:
online-store
- inner-store
-product-catalog
this is the resulting URI:
www.domain.com/online-store/inner-store/product-catalog
i would like to change it to:
www.domain.com/online-store/product-catalog
thought this might help but its not making any difference
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online-store/inner-store/\d+-(.+) /online-store/$1 [R=301,L]
i know its not considered good practice but i can't change the menu structure.
any suggestions ?
This regex \d+-(.+) will match 1 or more digits followed by hyphen followed 1 or more any thing
Try this code instead:
RewriteRule ^(online-store)/inner-store/(.*)$ /$1/$2 [R=301,L,NC]
Make sure this is first rule in your .htaccess and use a different browser to test it to avoid caching issues.

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.

Resources