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

WordPress中含连字符(-)的URL出现404错误的问题求助

Fixing 404 for Hyphenated URLs While Keeping the Hyphens Intact

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 last flag 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 .htaccess file, 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-blog to technology as 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 07:44:06