如何通过.htaccess移除PHP扩展名及URL中的blogs.html?title=部分
Hey there! Let's break down your two .htaccess questions with practical, working solutions. I’ve used these rules myself, so they should hold up for your setup.
First, double-check that your Apache server has mod_rewrite enabled (most shared hosts and local setups have this turned on by default). Then drop these rules into your root .htaccess file:
# Turn on rewrite engine RewriteEngine On RewriteBase / # Redirect users who visit .php URLs to the extension-less version (SEO-friendly 301 redirect) RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R=301,L] # Internally map extension-less URLs to their actual .php files (users won't see this change) RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [L]
Quick breakdown:
- The first rule handles external redirects: If someone types
yoursite.com/about.php, they’ll automatically be sent toyoursite.com/about(permanent 301 redirects help with search engine rankings). - The second rule does the behind-the-scenes work: When someone visits
yoursite.com/about, the server quietly servesabout.phpwithout showing the.phpextension in the URL.
You want to clean up http://localhost/omsi_site/blogs.html?title=Responsive%20web%20design to something like http://localhost/omsi_site/Responsive-web-design (cleaner and easier to share). Here’s how to set that up:
Add these rules to the .htaccess file inside your omsi_site folder (adjust RewriteBase if your directory structure is different):
RewriteEngine On RewriteBase /omsi_site/ # Redirect the old messy URL to the clean version RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/blogs\.html\?title=([^\s&]+) [NC] # Optional: Convert to lowercase and replace spaces with hyphens (uncomment if needed) # RewriteRule ^ %{lowercase:%1}? [R=301,L] RewriteRule ^ %1? [R=301,L] # Internally map the clean URL back to blogs.html with the title parameter RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)$ blogs.html?title=$1 [L,QSA]
What this does:
- The first rule redirects anyone using the old
blogs.html?title=...URL to the clean path. The?at the end strips any extra query parameters. If you prefer all-lowercase URLs, uncomment the optional line (note: this requiresmod_spelingenabled on your server). - The second rule makes sure when someone visits
http://localhost/omsi_site/Responsive-web-design, the server internally loadsblogs.html?title=Responsive-web-design— users never see the messy original URL. TheQSAflag preserves any extra query parameters you might add later.
Pro Tips:
- Clear your browser cache before testing! 301 redirects get cached, so old rules might stick around otherwise.
- If you already have existing .htaccess rules, place these above them to avoid conflicts.
- Make sure your Apache config allows
.htaccessoverrides (look forAllowOverride Allin the directory block for your site).
内容的提问来源于stack exchange,提问作者Alpesh

