Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

apache - Rewrite Rule Not Redirecting To The Specified File

Recently I started working with .htaccess file to redirect API call to a specific file. However, even though my .htaccess file is working (tested it with just text on top of the file and I get "Internal Server Error") my rule is not working. Instead, Index.php is always read. The goal is, whenever the url ends with "myOwnAPI/" it takes you to the file "somefile.php". All files(index.php, somefile.php, .htaccess) leave in the same directory.

The .htaccess file contains just one rule.

Please help.

httpd.conf file

<Directory />
    AllowOverride All
    Require all denied
</Directory>

AllowOverride All

.htaccess file

RewriteEngine on

RewriteRule   ^myOwnAPI/?$   somefile.php  [NC,L]
question from:https://stackoverflow.com/questions/65899394/rewrite-rule-not-redirecting-to-the-specified-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Every url start with a "/" so

RewriteRule   ^myOwnAPI/?$   somefile.php  [NC,L]

won't ever match. Change it to

RewriteRule   ^/myOwnAPI/?   /somefile.php  [NC,L]

Moreover, please read the offical documentation https://httpd.apache.org/docs/2.4/rewrite/flags.html if you need to "pass" the parameters to the new url (flag QSA).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...