在Apache中实现多URL重写:指定URL规则改写需求咨询
Hey there! Let's get those URL rewrites sorted out for you. Below are step-by-step solutions for both of your desired URL formats, covering both Apache (with mod_rewrite) and Nginx servers—since those are the most common setups for this kind of task.
.html 格式URL This will turn your original parameter-based URLs into clean root-level paths like https://www.myjohara.com/Rings.html.
Apache Configuration (mod_rewrite)
First, make sure mod_rewrite is enabled on your Apache server (run a2enmod rewrite on Debian/Ubuntu, then restart Apache). Then add these rules to your .htaccess file or Apache virtual host config:
RewriteEngine On # Permanent redirect from original URLs to clean root-level .html RewriteCond %{QUERY_STRING} ^jwelurl=([^&]+)$ RewriteRule ^jewelry/?$ /%1.html? [L,R=301] RewriteCond %{QUERY_STRING} ^caturl=([^&]+)$ RewriteRule ^collection/?$ /%1.html? [L,R=301] RewriteCond %{QUERY_STRING} ^purl=([^&]+)$ RewriteRule ^productdetails/?$ /%1.html? [L,R=301] # Internal rewrite: map the clean URLs back to your original application logic RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)\.html$ /jewelry?jwelurl=$1 [L] RewriteRule ^([^/]+)\.html$ /collection?caturl=$1 [L] RewriteRule ^([^/]+)\.html$ /productdetails?purl=$1 [L]
Quick Explanation:
R=301sends a permanent redirect signal to search engines and browsers, which is great for SEO. If you're testing, useR=302(temporary) first to avoid caching issues.- The
?at the end of the rewrite rule strips off the original query string from the redirected URL. - The internal rewrite rules tell Apache to serve the content from your original parameter-based URLs when someone visits the clean
.htmlpaths.
Nginx Configuration
Add these rules inside your server block in your Nginx config:
# Permanent redirect from original URLs to root-level .html rewrite ^/jewelry$ /$arg_jwelurl.html? permanent; rewrite ^/collection$ /$arg_caturl.html? permanent; rewrite ^/productdetails$ /$arg_purl.html? permanent; # Internal rewrite to map clean URLs to original application logic rewrite ^/([^/]+)\.html$ /jewelry?jwelurl=$1 last; rewrite ^/([^/]+)\.html$ /collection?caturl=$1 last; rewrite ^/([^/]+)\.html$ /productdetails?purl=$1 last;
After updating, restart Nginx to apply the changes.
.html 格式URL This setup will keep the subdirectory structure (like jewelry/, collection/) while cleaning up the URL, resulting in paths like https://www.myjohara.com/jewelry/Rings.html.
Apache Configuration (mod_rewrite)
Again, ensure mod_rewrite is enabled, then add these rules to .htaccess or your virtual host:
RewriteEngine On # Permanent redirect from original URLs to subdirectory .html RewriteCond %{QUERY_STRING} ^jwelurl=([^&]+)$ RewriteRule ^jewelry/?$ /jewelry/%1.html? [L,R=301] RewriteCond %{QUERY_STRING} ^caturl=([^&]+)$ RewriteRule ^collection/?$ /collection/%1.html? [L,R=301] RewriteCond %{QUERY_STRING} ^purl=([^&]+)$ RewriteRule ^productdetails/?$ /productdetails/%1.html? [L,R=301] # Internal rewrite: map subdirectory clean URLs to original logic RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^jewelry/([^/]+)\.html$ /jewelry?jwelurl=$1 [L] RewriteRule ^collection/([^/]+)\.html$ /collection?caturl=$1 [L] RewriteRule ^productdetails/([^/]+)\.html$ /productdetails?purl=$1 [L]
Nginx Configuration
Add these rules inside your server block:
# Permanent redirect from original URLs to subdirectory .html rewrite ^/jewelry$ /jewelry/$arg_jwelurl.html? permanent; rewrite ^/collection$ /collection/$arg_caturl.html? permanent; rewrite ^/productdetails$ /productdetails/$arg_purl.html? permanent; # Internal rewrite to map subdirectory clean URLs to original logic rewrite ^/jewelry/([^/]+)\.html$ /jewelry?jwelurl=$1 last; rewrite ^/collection/([^/]+)\.html$ /collection?caturl=$1 last; rewrite ^/productdetails/([^/]+)\.html$ /productdetails?purl=$1 last;
Restart Nginx after making changes to activate the rules.
Pro Tips:
- Always test redirects first with temporary 302 redirects before switching to 301, as browsers cache 301s aggressively.
- If you have other rewrite rules, make sure these are placed in the correct order (usually before more general rules).
- For Apache, ensure your
.htaccessfile is allowed to override settings (checkAllowOverride Allin your virtual host config).
内容的提问来源于stack exchange,提问作者Niyaz Ahmed

