CentOS7.4/Apache2.4.6下基于HTTP_REFERER的动态重定向配置咨询
Alright, let's get this redirect logic working for your CentOS 7.4 / Apache 2.4.6 setup. We'll use Apache's mod_rewrite module here—it's perfect for handling conditional redirects based on HTTP headers and file system checks.
First, Make Sure mod_rewrite is Enabled
Before diving into the rules, confirm mod_rewrite is active on your server:
httpd -M | grep rewrite
If you don't see rewrite_module (shared) in the output, enable it by editing your Apache config (usually /etc/httpd/conf/httpd.conf or /etc/httpd/conf.d/rewrite.conf):
LoadModule rewrite_module modules/mod_rewrite.so
Then restart Apache to apply the change:
systemctl restart httpd
The Rewrite Rules
You can add these rules either directly in your Apache server config (inside a <Directory> block for your website root) or in a .htaccess file in your site's root directory.
Here's the complete rule set:
RewriteEngine On # Match requests for /bb.html RewriteCond %{REQUEST_URI} ^/bb\.html$ [NC] # Extract the test file name (like test01.html) from the HTTP_REFERER # Adjust the regex here if your Referer URLs follow a different structure RewriteCond %{HTTP_REFERER} ^https?://example\.jp/.*/([^/]+\.html)$ [NC] # Store the captured test file name in an environment variable RewriteRule ^ - [E=TEST_FILE:%1] # Check if the target test file exists on the server RewriteCond %{DOCUMENT_ROOT}/bb/bb/homepage/%{ENV:TEST_FILE} -f # If it exists, redirect to that file (use R=301 for permanent redirects) RewriteRule ^/bb\.html$ /bb/bb/homepage/%{ENV:TEST_FILE} [R=302,L] # If the test file doesn't exist, redirect to homepage.html RewriteRule ^/bb\.html$ /bb/bb/homepage.html [R=302,L]
Let's Break Down Each Part
RewriteEngine On: Turns on the mod_rewrite engine—required for all rewrite rules.- First
RewriteCond: Ensures we only apply these rules when the user is accessing/bb.html([NC]makes the match case-insensitive). - Second
RewriteCond: Extracts the HTML filename (liketest01.html) from the HTTP Referer header. The regex assumes your Referer URLs look likehttp://example.jp/some/path/test01.html—adjust the regex if your Referer structure is different (e.g., if test files are always under a specific path). RewriteRule ^ - [E=TEST_FILE:%1]: Saves the captured filename to an environment variableTEST_FILEso we can reuse it later.- Next
RewriteCond: Checks if the file/bb/bb/homepage/{captured-file}exists on the server (-fmeans "is a regular file"). - First
RewriteRule: If the file exists, redirect the user to that path. UseR=301instead ofR=302if you want this to be a permanent redirect. The[L]flag tells Apache to stop processing further rules once this one is applied. - Final
RewriteRule: If the test file doesn't exist, this rule kicks in and redirects the user to/bb/bb/homepage.html.
Important Notes
- Document Root: CentOS 7.4's default Apache document root is
/var/www/html. If your site uses a different root, make sure%{DOCUMENT_ROOT}points to it, or replace it with the full absolute path to your site's root. - .htaccess Permissions: If using
.htaccess, ensure your Apache<Directory>block for the site root hasAllowOverride Allset—otherwise, the rules in.htaccesswon't be applied. - Testing: Use tools like
curlto test the redirects without a browser:# Test with a Referer that points to an existing test file curl -v -H "Referer: http://example.jp/foo/test01.html" http://example.jp/bb.html # Test with a Referer that points to a non-existent test file curl -v -H "Referer: http://example.jp/foo/test99.html" http://example.jp/bb.html
内容的提问来源于stack exchange,提问作者Akinobu Katou

