如何通过.htaccess隐藏URL中的knowledgebase.php?
Solution for Hiding knowledgebase.php in URLs
Here's the correct .htaccess configuration to meet your requirements:
RewriteEngine On # Redirect direct requests to knowledgebase.php to the root URL, preserving query parameters RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+knowledgebase\.php\s [NC] RewriteRule ^ /?%{QUERY_STRING} [R=301,L] # Internally rewrite root and non-file/directory requests to knowledgebase.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /knowledgebase.php [L,QSA]
How This Works:
Redirect Rule:
- The
RewriteCondchecks if the actual client request is targetingknowledgebase.php(case-insensitive, so it handles variations likeKnowledgeBase.phptoo). - The
RewriteRulesends a permanent redirect (R=301) to the root URL (/) while retaining any query parameters (like?article=1). For testing, useR=302temporarily to avoid browser caching issues.
- The
Rewrite Rule:
- The two
RewriteCondlines ensure we don't interfere with requests for real files or directories (so your static assets like CSS, images, or other pages load normally). - The
RewriteRuleinternally maps any non-file/directory request toknowledgebase.php. TheQSAflag appends the original query string to the rewritten URL, so/?article=1gets passed toknowledgebase.php?article=1behind the scenes without changing the visible URL.
- The two
Key Tips:
- Confirm
mod_rewriteis enabled on your Apache server (most hosting providers have this turned on by default). - Clear your browser cache before testing to avoid old redirects messing with your results.
- Once you verify everything works as expected, keep
R=301for permanent redirects to help with SEO.
内容的提问来源于stack exchange,提问作者c0dehunter
相关产品推荐
相关产品推荐

