Rewriting an URL with .htaccess

笑着哭i 提交于 2019-12-25 02:16:50

问题


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.



来源:https://stackoverflow.com/questions/22811917/rewriting-an-url-with-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!