简体   繁体   中英

.htaccess mod_rewrite not working. redirects to 404 error

I am working on a project for a client who has a blog/news page as part of their website. This is all fine however they have requested that they have custom permalinks instead of the standard issue php variables.

I have used the generator at searchfriendlyurls.com to create the rewrite rule. I have added this to my .htaccess file which is located in the root folder of the website however when I click on the link it just sends me to my hosts 404 error page. Any suggestions. .htaccess file below:

Options -Multiviews

RewriteEngine On
RewriteBase /

# Force search engines to use domain.example.org.uk
RewriteCond %{HTTP_HOST} !^domain\.example\.org\.uk$
RewriteRule ^(.*) http://domain.example.org.uk/$1 [R=301,L]

# Specify search friendly URLs
RewriteRule ^media/news/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]

# Generated for free at SearchFriendlyURLs.com

Any suggestions would be much appreciated

I noticed that your article.php might be expecting numbers judging by your query article.php? article_url_year= $1& article_url_month= $2 however your RewriteRule RewriteRule ^media/news/ ([az]+) will only match letters.

Try to replace this line:

RewriteRule ^media/news/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]

With this:

RewriteRule ^media/news/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]

And see if it works.

This code will only allow:

  • All letters (uppercase and lowercase).
  • All numbers.
  • Dash (-)

Adding another regular expression variation to the mix:

RewriteRule ^media/news/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]

eg for URL PATH: /media/news/2012/11/test-article/1

The rewrite log, if enabled, will show:

[rid#7f920b4b00a0/initial] (3) [perdir /srv/www/htdocs/] applying pattern '^media/news/([^/]+)/([^/]+)/([^/]+)/([^/]+)$' to uri 'media/news/2012/11/test-article/1'
[rid#7f920b4b00a0/initial] (2) [perdir /srv/www/htdocs/] rewrite 'media/news/2012/11/test-article/1' -> '/media/news/article.php?article_url_year=2012&article_url_month=11&article_url_title=test-article&article_id=1'
[rid#7f920b4b00a0/initial] (3) split uri=/media/news/article.php?article_url_year=2012&article_url_month=11&article_url_title=test-article&article_id=1 -> uri=/media/news/article.php, args=article_url_year=2012&article_url_month=11&article_url_title=test-article&article_id=1
[rid#7f920b4b00a0/initial] (2) [perdir /srv/www/htdocs/] trying to replace prefix /srv/www/htdocs/ with /
[rid#7f920b4b00a0/initial] (1) [perdir /srv/www/htdocs/] internal redirect with /media/news/article.php [INTERNAL REDIRECT]
[rid#7f920bcdda00/initial/redir#1] (3) [perdir /srv/www/htdocs/] strip per-dir prefix: /srv/www/htdocs/media/news/article.php -> media/news/article.php
[rid#7f920bcdda00/initial/redir#1] (3) [perdir /srv/www/htdocs/] applying pattern '^media/news/([^/]+)/([^/]+)/([^/]+)/([^/]+)$' to uri 'media/news/article.php'
[rid#7f920bcdda00/initial/redir#1] (1) [perdir /srv/www/htdocs/] pass through /srv/www/htdocs/media/news/article.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM