I am in desperate need of some help here. I’ve been pulling my hair out for over a week now regarding this issue. I know that it should be incredibly straight forward, but for some reason it just doesn’t work…
I have a search query that I would like to make to look pretty for aesthetic and seo reasons. I want /rentals to be internally redirected to the (ugly) query /?ct_ct_status=for-rent&search-listings=true. Just to clarify here, I want visitors not to notice that when they visit /rentals, that they are actually being redirected to the query.
Anyway, I have tried the following:
RewriteEngine On
RewriteBase /
RewriteRule ^rentals/?$ https://www.example.com/?ct_ct_status=for-rent&search-listings=true [L]
Now here example.com is obviously not the actual url. I have tried the above and many minor variations of it including: only the query and not full url; different flags; with and without RewriteEngine On; with and without RewriteBase; trying to redirect not to a query but to a regular page; and probably some other things that I forgot about.
Now it is also worth mentioning that this website runs on WordPress. The above rewriterule is placed above WordPress’ section in the .htaccess, but that doesn’t work either.
If you would like me to provide more info, please let me know!
Thank you for your help!
Edit:
To answer @msg questions:
I should have mentioned this in my initial question. The RewriteRule mentioned above, does redirect to the correct page. However it is an external rewrite. In other words, when visitings /rentals it does redirect to https://www.example.com/?ct_ct_status=for-rent&search-listings=true, but it shows the redirect as the url and thus doesn’t show /rentals as the url. The goal is to make it an internal rewrite, so that when someone visits /rentals, it actually shows /rentals as the url, and not the redirect.
I have also tried the [R] flag, which Apache’s website says is the right flag to use for internal rewrites: link. But this still results in an external rewrite.
The query is not handled by WordPress. The PHP file that handles the homepage checks, if there is a search query present and if so, it shows the output of the PHP file that handles search results.
To answer @shane questions:
The answer I gave to one of @msg questions above might help answer your questions too. But to expand on that, rentals is actually not a custom post type. It is a taxonomy for a custom post type called ‘listings’.
function add_custom_rewrite_rule() {
add_rewrite_rule('^rentals/?$', 'https://www.redcapper.com/?ct_ct_status=for-rent&search-listings=true', 'top');
}
add_action('init', 'add_custom_rewrite_rule', 10, 0);
I have tried hooking into WordPress’ rewrite API using the above, but it doesn’t work… It gives me a 404 error…
2
Answers
Rewriting of WordPress URLs should not be done in .htaccess – WordPress itself implements it’s own rewriting engine.
All rewriting should be done with this API. See add_rewrite_rule.
EDIT
Try this…
Sorry but you can’t do that (as far as I know). External Redirects are exactly that: Redirects. Masking only works with internal redirects. According to the documentation:
So, whether or not you specify the
[R]
flag, if the target domain is not the same, it always results in the URL on the browser bar changing.You can Redirect to the new domain with:
This will result in a redirection, but the browser bar will show
http://example.com/rentals
instead of the query string one. In the target system you already have a wordpressrewrite_rule
so that should work.This is a compromise between what you currently have and what you want.
What you are really trying to do is proxy a request, and
mod_rewrite
on its own can’t do it. It is not as straightforward as you may think, there are many security considerations where the client might not trust the target system, or there are session cookies or other potential information disclosure involved.However, if the middle ground is not acceptable, you could look into
mod_proxy
. There’s support for proxying requests withmod_rewrite
and the[P]
flag but you have that prerequisite. But that’s a setup I haven’t used and can’t help much more with.If you can modify the code you could consider scrapping the target page, but that looks like too much work.
Lastly, you can just embed the target page in your current one.