Hi lads,
I’m in need of some help with a tricky Apache rewrite situation I’m facing. I have multiple subdomains and subdirectories with various access requirements, and I want to create a rewrite rule that satisfies a couple of conditions.
Basically, if :
subdomain.example.com
/files/
..pdf
or .jpg
.public_
prefix.If all the above conditions are met, the request should be redirected to a separate domain https://public-files.example.net/
.
I’ve tried the following rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} ^/files/public_.*\.(pdf|jpg)$
RewriteRule ^/files/public_(.*)\.(pdf|jpg)$ https://public-files.example.net/$1.$2 [R=301,L]
ofcourse with my domains but it doesn’t work. I believe it might be conflicting with other rewrite rules in my configuration.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Heya,
I’ve looked into your rewrite rule, and it seems mostly correct. However, there are a couple of things that could be improved to make it work as expected. Here’s an updated version of the rule:
I removed the
RewriteCond
for the%{REQUEST_URI}
because it’s not necessary since the RewriteRule pattern will only match if the conditions are already met. I also simplified theRewriteRule
pattern and included thepublic_
prefix in the replacement URL.I’ve tested it on my end and it works as you’ve described but do let me know how it goes with you