3

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

URL structure is the following:

example.com/ex.php?ez=DF004AE

It must become:

example.com/ex/DF004AE.htm

What is the correct script to add to my .htaccess in order to do that?

3
  • Have you tried anything ? Commented Jul 12, 2016 at 10:55
  • Does the new url exist? Commented Jul 12, 2016 at 11:12
  • no, it's just a pretty link for SEO Commented Jul 12, 2016 at 11:20

3 Answers 3

2

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 will convert /ex.php?ez=foobar into /ex/foobar.htm .

Sign up to request clarification or add additional context in comments.

Comments

1

Use this:

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

It will give you the following URL:

example.com/ex/DF004AE.htm

If you meant it to be .html (not .htm) Just add the l in the RewriteRule.

2 Comments

Again, I thought the question was asking the opposite, i.e.f from the query string URL to the pretty one.
Exactly how I read it @apokryfos ! The OP has asked for the URL example.com/ex.php?ez=DF004AE to become example.com/ex/DF004AE.htm.
0
RewriteEngine On
RewriteRule ^ex/([^/]*)\.html$ /ex.php?ez=$1 [R=301,NE,L]

If you don't want the address bar to reflect this change, remove R=301. The NE parameter is there to make sure that the request is not escaped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.