简体   繁体   中英

omitting “public” from URL using .htaccess rewrite

I have a current REST API where URL's used to look like following:

example.com/public/products
example.com/public/products/123

I have updated the URL structure to:

example.com/products
example.com/products/123

The issue is that there are current users of this REST API that only connect to the 'public' URL, so this update will give them 404 error. How can I add a rule to my rewrite so GET requests to URL with "public" get processed without "public" correctly.

example.com/public/products -> example.com/products
example.com/public/products/123 -> example.com/products/123

How can I write a rewrite rule that does this for me?

.htaccess:

RewriteEngine On
RewriteRule ^public/(.*)$ $1 [R=301,L]

You can test it here: http://htaccess.madewithlove.be/


Regular Expression:

^
Matches beginning of string (the URL path in this case).

public/
Matches the string "public/".

(.*)
Parentheses make this a capturing group; . matches any character except line breaks; * matches zero or more of preceding token.

$
Matches end of string.


Output URL (with capture group reference numbers):

$1 : Insert the contents of capture group #1


Options:

[R=301,L] : Performs a 301 [R]edirect and indicates this is the [L]ast rule thus stopping the rewriting process immediately .


Apache Module mod_rewrite documentation: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM