mod_rewrite appends query string when not using trailing slash

淺唱寂寞╮ 提交于 2020-01-15 12:05:14

问题


I've spent some time on Google and SO trying to figure this out and I have now had to resort to asking yet another mod_rewrite question....

I am using mod_rewrite and for the most part it works fine.

The part I am having problems with is:

# Load pages for health-it while keeping the URL masked
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^health-it/([-a-z0-9]+)/? /health-it/loadPage.php?p=$1 [NC,L]

I have a URI that I want:

/health-it/aimsconsultant

The problem is, mod_rewrite is doing this:

/health-it/aimsconsultant/?p=aimsconsultant

The page still loads but the URI is the issue.

/health-it/aimsconsultant/

Works as expected. It should be noted that /health-it/aimsconsultant is a directory and maybe this is the cause? I believe that Apache redirects to append the trailing slash for directories but why is it appending the query string?

It works perfect for internal pages (no directory for internal pages):

/health-it/aimsconsultant/manufacturer-signup

and

/health-it/aimsconsultant/manufacturer-signup/

work properly.

The only issue is on the first level... This makes no sense to me and I am baffled as to why this is happening. Please, can someone enlighten me on how to fix this and why this is working in this manner?

Thanks for your help


回答1:


OK, sorry, I misread the ? for a $ !

Tried adding this, it seems to to the job:

DirectorySlash Off

By default, it's On, and that's why when Apache tries to get the canonical representation of your URL, it falls back onto the directory.




回答2:


Add a specific entry for the URL version without trailing slash:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^health-it/([-a-z0-9]+)? /health-it/loadPage.php?p=$1 [NC,L]

I've already had this kind of problem, where mod_rewrite ends with appending the query string, even without QSA flag. Don't know the reason why...



来源:https://stackoverflow.com/questions/9704592/mod-rewrite-appends-query-string-when-not-using-trailing-slash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!