rewrite rule not working: redirect to php script if file exists

I wanted to use add_rewrite_rule but had no success.What I want to achieve is that if the URL www.myhomepagecustom-medi

I wanted to use add_rewrite_rule but had no success.

What I want to achieve is that if the URL www.myhomepage/custom-media-files/2/screenshot_15_1.jpg is opened, that the user is redirected to a php script located in www.myhomepage/screenshotaccess.php where I check if the user has the permission to open the file.

Edit: So i tried to edit htaccess file it now looks likes this, but it still not working. It seems that the rule does not apply when a file is stored directly behind the URL. If i go to a URL where no file is located, the rule works.

#BEGIN screenshotaccess
RewriteEngine On
RewriteRule ^custom-media-files/$  [L]
RewriteRule ^custom-media-files/([0-9]+)/$  [L]
#RewriteRule ^custom-media-files/(.+)/screenshot_trade_(.+)_(.+)\.jpg$  [L]
RewriteRule ^custom-media-files/(.+)/screenshot_trade_(.+)_(.+)\.jpg$ /screenshotaccess.php?usrid=$1&fn=$2&i=$3 [L]

#END screenshotaccess

I wanted to use add_rewrite_rule but had no success.

What I want to achieve is that if the URL www.myhomepage/custom-media-files/2/screenshot_15_1.jpg is opened, that the user is redirected to a php script located in www.myhomepage/screenshotaccess.php where I check if the user has the permission to open the file.

Edit: So i tried to edit htaccess file it now looks likes this, but it still not working. It seems that the rule does not apply when a file is stored directly behind the URL. If i go to a URL where no file is located, the rule works.

#BEGIN screenshotaccess
RewriteEngine On
RewriteRule ^custom-media-files/$ https://google [L]
RewriteRule ^custom-media-files/([0-9]+)/$ https://google [L]
#RewriteRule ^custom-media-files/(.+)/screenshot_trade_(.+)_(.+)\.jpg$ https://google [L]
RewriteRule ^custom-media-files/(.+)/screenshot_trade_(.+)_(.+)\.jpg$ /screenshotaccess.php?usrid=$1&fn=$2&i=$3 [L]

#END screenshotaccess
Share Improve this question edited Jun 6, 2019 at 10:10 Michael H. asked Jun 4, 2019 at 14:48 Michael H.Michael H. 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

The reason the rewrite rules wont work is because you'll find that if you look at your .htaccess file, WordPress doesn't actually load if the file that is being requested is present on-disk.

So if they try to access an image file, because it's there, on disk, WordPress isn't loaded and so the rewrite rules have no affect.

You may want to use .htaccess rules. Here is an example of what you could try:

RewriteEngine On
RewriteRule ^custom-media-files/(.+)/screenshot_trade_(.+)_(.+)\.jpg$ /screenshotaccess.php?usrid=$1&fn=$2&i=$3 [L]

I'd also make sure in your screenshotaccess.php that you're not just using your query/$_GET vars without first checking they're of the correct format - i.e. you're santizing your data before you use it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745432496a4627456.html

相关推荐

  • rewrite rule not working: redirect to php script if file exists

    I wanted to use add_rewrite_rule but had no success.What I want to achieve is that if the URL www.myhomepagecustom-medi

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信