Rewriting an URL with .htaccess

2019-09-06 05:06发布

问题:

After facing many problems with the htaccess rewrite rule as it is at the moment I have decided to change it to something more basic but I cannot get the end page to pick up the ID.

My .htaccess is:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /article\.php\?issue=(.*)&edition=(.*)&id=(.*)&title=(.*)\ HTTP
RewriteRule ^ /article/%2/%3/%4/%5\? [R=302,L]
RewriteRule ^article/(.*)/(.*)/(.*)/(.*)$ /article.php?issue=$1&edition=$2&id=$3&title=$4 [L]

How do I get the article page to pick up the ID number?

回答1:

You can use this code:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} \s/article\.php\?issue=([^&]*)&edition=([^&]*)&id=([^&]*)&title=([^&\s]*)
RewriteRule ^ /article/%1/%2/%3/%4? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /article.php?issue=$1&edition=$2&id=$3&title=$4 [L,QSA,NC]

Use of -MultiViews is very important here. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.



回答2:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /article.php?      issue=$1&edition=$2&id=$3&title=$4 [L]

you can use this site

http://www.generateit.net/mod-rewrite/index.php



回答3:

The following should work:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /article\.php\?issue=(.*)&edition=(.*)&id=(.*)&title=(.*)\ HTTP
RewriteRule ^ /article/%2/%3/%4/%5\? [R=302,L]

RewriteRule ^article/(.*)/(.*)/(.*)/(.*)$ /article.php?issue=$1&edition=$2&id=$3&title=$4 [L]

It should change
http://website.local.co.uk/article.php?issue=1&edition=leeds&id=1392214680-926677045&title=dearly-noted-presents---lace-and-leather
to
http://website.local.co.uk/article/1/leeds/1392214680-926677045/dearly-noted-presents---lace-and-leather

EDIT:

<?php 
    echo $_GET["issue"];
    echo $_GET["edition"];
    echo $_GET["id"];
    echo $_GET["title"];
?>

You can change R=302 to R=301 when you are sure the redirect works correctly.