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

.htaccess URL Rewrite Rule Not Working In Subdirectory of Existing WordPress Site on Apache - Stack Overflow

programmeradmin1浏览0评论

I am trying to get a redirect rule working in a subfolder so I can rewrite a URL from:

.php?id=1

To (which is how users would access the above to provide a more friendly URL):


Where:

  • Name of php file is the first variable so could have /api/OtherObject/1
  • Number on end is the Id of the record

This is the content of the .htaccess file where I only want to affect this sub-folder. When using online testers they seem to suggest the rule works until I add the RewriteBase value but I have tried adding the main rewrite rule to the top level .htaccess too as the first rule with an [L] tag on the end but I still cannot get it to work. Rewrite works in general on the server so I don't think it is a hosting issue but not sure if WordPress is causing any other conflict in the parent folder.

These are the contents of the .htaccess file:

RewriteEngine On
RewriteBase /api/
RewriteRule ^api/([a-zA-Z0-9_-]+)/?([0-9]+)?$ /api/$1.php?id=$2 [NC,QSA,L]

Thanks Robin

I am trying to get a redirect rule working in a subfolder so I can rewrite a URL from:

https://www.somesite.co.uk/api/FormEntries.php?id=1

To (which is how users would access the above to provide a more friendly URL):

https://www.somesite.co.uk/api/FormEntries/1

Where:

  • Name of php file is the first variable so could have /api/OtherObject/1
  • Number on end is the Id of the record

This is the content of the .htaccess file where I only want to affect this sub-folder. When using online testers they seem to suggest the rule works until I add the RewriteBase value but I have tried adding the main rewrite rule to the top level .htaccess too as the first rule with an [L] tag on the end but I still cannot get it to work. Rewrite works in general on the server so I don't think it is a hosting issue but not sure if WordPress is causing any other conflict in the parent folder.

These are the contents of the .htaccess file:

RewriteEngine On
RewriteBase /api/
RewriteRule ^api/([a-zA-Z0-9_-]+)/?([0-9]+)?$ /api/$1.php?id=$2 [NC,QSA,L]

Thanks Robin

Share Improve this question edited Nov 20, 2024 at 10:32 ADyson 62.1k16 gold badges78 silver badges91 bronze badges asked Nov 20, 2024 at 10:07 Robin WilsonRobin Wilson 3903 silver badges12 bronze badges 4
  • That rule should work and testers confirm that. So there has to be some other reason why you say this does not work. What is your actual issue, so the faulty result you see? – arkascha Commented Nov 20, 2024 at 17:25
  • Besides: what sense is there in the NC flag if you use a matching pattern including lower and upper case characters? – arkascha Commented Nov 20, 2024 at 17:27
  • The solution is to remove the RewriteBase /api/. – arkascha Commented Nov 20, 2024 at 18:58
  • @arkascha The issue is that if I go to that URL I get a 404 Not Found error so it isn't working and if I try it without the RewriteBase command then it still doesn't work. If I only want to apply it to this folder I thought it was perhaps safer to add this command but isn't working with or without that. I was just trying all flags on and off to see if it made a difference. – Robin Wilson Commented Nov 21, 2024 at 10:14
Add a comment  | 

2 Answers 2

Reset to default 0

I have finally managed to fix it and this combination is working with the `.htaccess' file in the subfolder:

Options +FollowSymLinks -MultiViews
DirectoryIndex FormEntries.php

RewriteEngine On
RewriteBase /api/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-zA-Z0-9_-]+)/?([0-9]+)?$ /api/$1.php?id=$2 [NC,QSA,L]

So now /api/FormEntries/1 goes to /api/FormEntries.php?id=1 successfully showing the output from the API in a more user friendly way.

I have left in the flags as NC is useful for not being case sensitive so it will still work if all lowercase, QSA is useful to pass on any additional query parameters that are added and L to avoid processing other rules if this one matches so all seems to be doing what I had hoped it would do.

If the .htaccess file is at the root of the api directory, you don't want to reference it again in your rules or it will try to find a api directory inside api directory, which is not the case and obviously leads to a 404 page.

So this one line htaccess inside api directory will work as I tested in a WordPress with your file structure in the below comment.

RewriteRule ^(.*)/(.*)                 $1.php?id=$2 [QSA,L]
发布评论

评论列表(0)

  1. 暂无评论