WordPress中含连字符(-)的URL出现404错误的问题求助
Hey there! I get it—you want to keep that hyphenated URL (https://www.domainlist.com/blog/filter/technology-blog/) working without ditching the hyphens, even though the non-hyphenated version (https://www.domainlist.com/blog/filter/security/) loads fine. Let’s walk through practical, actionable solutions tailored to common scenarios:
1. Server-Side Rewrite Rules (Apache/Nginx)
This is the quickest fix if you’re using a traditional web server. The goal is to internally forward the hyphenated URL request to the working non-hyphenated path—users will see the hyphenated URL in their address bar, but get the correct content.
For Apache (using .htaccess)
Add this rule to your site’s .htaccess file (ensure mod_rewrite is enabled on your server):
# Forward technology-blog to technology internally (no browser redirect) RewriteRule ^blog/filter/technology-blog/$ /blog/filter/technology/ [L]
- The
[L]flag tells Apache to stop processing further rules once this one matches. No redirect happens behind the scenes—users stay on the hyphenated URL.
For Nginx
Add this snippet to your server block configuration:
location /blog/filter/technology-blog/ { rewrite ^/blog/filter/technology-blog/$ /blog/filter/technology/ last; }
- The
lastflag makes Nginx process the rewritten path as a new internal request, so the user never sees the non-hyphenated URL.
2. Adjust Backend Routing Logic
If you’re running a custom application (like Node.js/Express, Django, etc.), modify your route handler to clean up hyphenated parameters before fetching content:
Example: Node.js/Express
app.get('/blog/filter/:category', (req, res) => { // Remove all hyphens from the category parameter const cleanCategory = req.params.category.replace(/-/g, ''); // Use the cleaned category to fetch your blog content fetchBlogContent(cleanCategory) .then(content => res.render('blog-filter', { content })) .catch(err => res.status(404).send('Not Found')); });
Example: Django
First, update your URLconf to accept hyphenated slugs:
from django.urls import path from . import views urlpatterns = [ path('blog/filter/<slug:category>/', views.blog_filter_view), ]
Then strip hyphens in your view to match internal category names:
def blog_filter_view(request, category): clean_category = category.replace('-', '') # Fetch content using the cleaned category string # ... rest of your view logic
3. CMS-Specific Fixes (e.g., WordPress)
If you’re using a CMS like WordPress:
- Check Permalink Settings: Head to Settings > Permalinks. If you’re using a custom structure, confirm it supports hyphenated slugs (most do by default).
- Custom Rewrite Rule: Add the Apache rule from Section 1 to your WordPress
.htaccessfile, placing it just below the default WordPress rewrite block. - Plugin Help: Plugins like Redirection let you create internal forwardings without editing config files—set up a rule to map
technology-blogtotechnologyas an internal redirect.
4. Universal Catch-All for Multiple Hyphenated URLs
If you have more than one hyphenated URL to fix, use a generic rewrite rule to strip hyphens automatically:
Apache
# Strip hyphens from any two-word category in the filter path RewriteRule ^blog/filter/([^/]+)-([^/]+)/$ /blog/filter/$1$2/ [L]
This works for URLs like marketing-tips → marketingtips—adjust the regex if you have longer hyphenated strings.
Nginx
rewrite ^/blog/filter/([^/]+)-([^/]+)/$ /blog/filter/$1$2/ last;
Start with server rewrite rules if you can—they’re usually the fastest fix without touching application code. If you’re working with a specific tech stack, feel free to ask for more tailored details!
内容的提问来源于stack exchange,提问作者Abhishek Joshi

