通过.htaccess配置URL重写规则,移除网站非SEO友好的?Script=前缀
?Script= URLs with Apache mod_rewrite Got it, let's fix those SEO-unfriendly ?Script= URLs once and for all. Since you already have your news article rewrite working, extending this to all your pages is straightforward—assuming you're using Apache's mod_rewrite (if you're on Nginx, just let me know and I can adjust the rules!).
Step 1: Keep Your Existing News Rule (Priority First!)
First, leave your existing news article rewrite rule at the top—we want to handle those specific URLs before the generic page rules to avoid conflicts:
RewriteEngine On # Existing news article rewrite rule (keep this first!) RewriteRule ^news/([0-9a-zA-Z_-]+)$ ?Script=news_view&uid=$1 [L,QSA]
Step 2: Add the Generic Page Rewrite Rule
Next, add a rule that maps clean URLs like example.com/about to the old ?Script=about format the server understands. We'll add conditions to skip real files/directories (so your CSS, JS, images don't get broken):
# Generic rule for all other pages (maps /page-name to ?Script=page-name) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([0-9a-zA-Z_-]+)$ ?Script=$1 [L,QSA]
Step 3: Add 301 Redirect for Old URLs (Critical for SEO!)
To avoid duplicate content and pass SEO weight from old URLs to new ones, add a permanent 301 redirect rule. This will automatically send anyone visiting example.com/?Script=about to example.com/about:
# 301 Redirect old ?Script= URLs to clean URLs (preserves SEO value) RewriteCond %{QUERY_STRING} ^Script=([0-9a-zA-Z_-]+)$ RewriteRule ^$ /%1? [R=301,L]
Let's Break Down the Key Parts
RewriteCond %{REQUEST_FILENAME} !-f/!-d: Ensures we only rewrite requests for non-existent files/directories—so real static resources (likestyle.cssorimages/logo.png) aren't affected.[L]: Marks the rule as "last"—once matched, no further rules are processed. That's why we put the news rule first (we don't want it getting caught by the generic rule).[QSA]: Stands for "Query String Append"—if someone adds extra parameters (likeexample.com/about?foo=bar), it'll pass those to the rewritten URL (?Script=about&foo=bar).- The 301 redirect's
?at the end: Clears the original query string so the new URL doesn't have leftover?Script=...garbage.
Important Notes
- Make sure mod_rewrite is enabled on your Apache server. You can enable it with
a2enmod rewrite(on Debian/Ubuntu) or check your Apache config forLoadModule rewrite_module. - If you're using
.htaccess, ensure your virtual host config hasAllowOverride Allset for your website's directory—otherwise the.htaccessrules won't apply. - Test thoroughly: Use your browser's dev tools (Network tab) to check that old URLs redirect with a 301 status, and new URLs load the correct page without errors.
- If your page titles include special characters (like accented letters or non-Latin characters), adjust the regex from
[0-9a-zA-Z_-]+to something more flexible (e.g.,[\w\-À-ž]+for extended Latin), but keep URLs as clean as possible for SEO.
内容的提问来源于stack exchange,提问作者Ihfaz

