You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过.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.

1. Removing PHP Extensions from URLs

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 to yoursite.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 serves about.php without showing the .php extension in the URL.
2. Optimizing the Blogs URL Structure

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 requires mod_speling enabled on your server).
  • The second rule makes sure when someone visits http://localhost/omsi_site/Responsive-web-design, the server internally loads blogs.html?title=Responsive-web-design — users never see the messy original URL. The QSA flag 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 .htaccess overrides (look for AllowOverride All in the directory block for your site).

内容的提问来源于stack exchange,提问作者Alpesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:04:56