Greg Ostry
Greg Ostry

Reputation: 1231

How to rewrite the folder path with RewriteRule command in .htaccess

I have some problems with understanding the URL rewriting rule.

My URL is

http://example.com/katalog/selection/book.php

and I would like to change the URL with RewriteRule to

http://example.com/selection

my code:

RewriteEngine On
RewriteRule ^http://example.com/katalog/selection/book.php$ http://example.com/selection [R=301,L]

but I can't access the new path.

Upvotes: 1

Views: 126

Answers (1)

MrWhite
MrWhite

Reputation: 45829

As CBroe mentioned in comments, the RewriteRule pattern matches the the URL-path only, not the scheme+hostname+path. However, "but I can't access the new path" suggests that you want to internally rewrite the request from /selection to /katalog/selection/book.php - which is the complete opposite of what you are trying to do. (Although /selection looks a bit too general?)

Try something like the following in your root .htaccess file.

RewriteEngine On
RewriteRule ^selection$ katalog/selection/book.php [L]

The RewriteRule pattern does not start with a slash in per-directory .htaccess files.

Now request the URL /selection and the request will be internally rewritten (visible web address does not change) to the file /katalog/selection/book.php.

Note you will need to clear your browser cache before testing, as 301s are cached hard by the browser.

Upvotes: 1

Related Questions