Вопрос

I want to change:

localhost/article.php?id=bryan

To simply:

localhost/article/bryan

And if it's something like:

localhost/article.php?id=Mark%20Henry%20is%20Drawing

I need that to change that to just:

localhost/article/mark-henry-drawing

A fellow here gave me this code to use:

RewriteEngine On
RewriteRule ^article/([^/]*)$ /article.php?id=$1 [L]

Everyone is saying that this should do the trick, but I'm still getting internal server errors every time I type in:

localhost/article/bryan

I'm using WAMP server if that helps at all, the following code is my entire .htaccess file:

Options +FollowSymLinks
RewriteEngine On

# Removes the .php extension from pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

# Cleans up the PHP URL of all pages on website
RewriteRule ^article/([^/]*)$ /article.php?id=$1 [L]

I don't know what else to try.

Это было полезно?

Решение

Well the problem with your current .htaccess is that its going into a loop causing the internal server error.

To make what you wish work you need to capture the URL at the request and redirect it, and then internally redirect it back:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect /article.php?id=bryan to /article/bryan
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+article\.php\?id=([^\s]+) [NC]
RewriteRule ^ /article/%1? [R=302,L]

# Internally forward /article/bryan to /article.php?id=bryan
RewriteRule ^article/([^/]+)/?$ /article.php?id=$1 [L,NC]

However there is no easy way to convert %20 into spaces or - you will have to work that out from your system.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top