Using Mod Rewrite to Change URL Structure

I want to use mod rewrite via htaccess for a PHP website.

The structure of the URL is as follows:

example.com/ex.php?ez=DF004AE

He must become:

example.com/ex/DF004AE.htm

What is the correct script to add to my .htaccess for this?

+4
source share
3 answers

Use this:

RewriteEngine On
RewriteRule ^ex/([^/]*)\.htm$ /ex.php?ez=$1 [L]

It will provide you with the following URL:

example.com/ex/DF004AE.htm

If you meant this .html (not.htm) Just add l to the RewriteRule.

+2
source

You can use the following rules:

RewriteEngine on
# Redirect from "/ex.php?ez=foobar" to "/ex/foobar.htm"
RewriteCond %{THE_REQUEST} /ex\.php\?ez=([^\s]+) [NC]
RewriteRule ^ /ex/%1.htm? [L,R]
####################
# internally map "/ex/foobar.htm" to "/ex.php?ez=foobar"
RewriteRule ^ex/([^.]+)\.htm$ /ex.php?ez=$1 [NC,L]    

This converts /ex.php?ez=foobarto /ex/foobar.htm.

+2
source
RewriteEngine On
RewriteRule ^ex/([^/]*)\.html$ /ex.php?ez=$1 [R=301,NE,L]

If you do not want the address bar to reflect this change, delete R = 301. The NE parameter must ensure that the request is not escaped.

+1
source

All Articles