Update
I found this post, it explains RewriteRule
can be used in .htaccess
but it needs to be modified.
This works, but it needs to be the second rule under RewriteEngine On
RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [L]
This works well for URLs like this
/find-a-home/new-homes/greenbooth/property-type/bower
But how would I modify it to handle URLs with query strings?
/find-a-home/new-homes/greenbooth-village/property-types?property_beds=&property_type_price=60000
I've made this htaccess rule
RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [R=301,L]
I've tested it here https://htaccess.madewithlove.com?share=ebd5828d-9535-4ae9-b74a-1b70a19535a2
The htaccess.madewithlove.com validator says it works.
However, when I update demo.com
to my actual url
and put it on my server, it doesn't work?
I checked the server logs and I can see this error
2022-06-14 10:27:37.449888 [INFO] [515989] [T0] [HTAccess] [/home/wwwsite/public_html/.htaccess:7] Redirect URL [^/find-a-home/new-homes/greenbooth/(.*)] does not fall inside current context [/], ignore.
What does does not fall inside current context
mean and is there a way to update the RewriteRule
so it does fall inside the current context?
I'm trying to catch multiple pages with one rule. I have a bunch of pages like this
https://demo.com/find-a-home/new-homes/greenbooth/property-types/gilson
https://demo.com/find-a-home/new-homes/greenbooth/property-types/holtham
https://demo.com/find-a-home/new-homes/greenbooth/property-types/howarth
I'd like to catch any url that contains find-a-home/new-homes/greenbooth
and redirect to https://demo.com/new-homes
From what I've read, Redirect 301
doesn't accept regex expressions - that's why I'm using a RewriteRule. Although it feels like I need to add something else to make it work.
Can anyone help?
.htaccess
file. The rest of the rules work.
# Perch Runway
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /perch/core/runway/start.php [L]
# doesn't work, doesn't error?
RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [R=301,L]
Redirect 301 /index.htm /
Redirect 301 /portfolio.htm /find-a-home/new-homes
Redirect 301 /buyers.htm /
Redirect 301 /land.htm /
CodePudding user response:
RewriteCond %{REQUEST_URI} !^/perch RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* /perch/core/runway/start.php [L] # doesn't work, doesn't error? RewriteRule ^/?find-a-home/new-homes/greenbooth/?(.*)$ https://demo.com/new-homes [R=301,L]
These two rules are in the wrong order. A request for /find-a-home/new-homes/greenbooth/<anything>
(which I assume does not map to a physical file) is first rewritten to /perch/core/runway/start.php
. The second rule therefore never matches and does nothing.
Generally, external redirects need to go before internal rewrites.
These two rules can also be simplified in a couple of ways. You are not using a backreference in the substitution string so there is no need for the capturing subpattern (ie. (.*)
). And the condition that checks that the URL does not already start perch
should be moved to the RewriteRule
pattern (the additional condition is not required).
For example:
# Redirects
RewriteRule ^find-a-home/new-homes/greenbooth https://example.com/new-homes [R=301,L]
# Front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !perch /perch/core/runway/start.php [L]
The regex ^find-a-home/new-homes/greenbooth
matches all URLs that start with that string (there is no slash prefix in .htaccess
).
You should test first with 302 (temporary) redirects to avoid potential caching issues.
But how would I modify it to handle URLs with query strings? /find-a-home/new-homes/greenbooth-village/property-types?property_beds=&property_type_price=60000
That depends what you mean. The above rule will already "handle URLs with query strings", in that they will be redirected. The rule above would redirect the stated URL. However, the query string is preserved by default across the redirect. If you want to remove the query string from the redirect response then include the QSD
(Query String Discard) flag on the redirect.
For example:
RewriteRule ^find-a-home/new-homes/greenbooth https://example.com/new-homes [QSD,R=301,L]