最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论