htaccess pretty URL with PHP & URL variables

独自空忆成欢 提交于 2020-01-11 13:19:12

问题


hoping someone can offer some assistance here. This is an issue with multiple layers. In short, I want to have pretty URLs that use a URL variable to a file within a folder.

So, I want http://www.example.com/?page=path/to/page to look like http://www.example.com/path/to/page/

On the index.php page, to load the above content:

require_once('content/'.$_GET['page'].'.php');

Current htaccess configuration:

RewriteEngine On

# Add trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R=301]

# Make URLs sexy!
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

I can get this to work with a top level page within the /content folder, however, when the php file I with to include is within a folder inside the /content folder, it does not work.

Any ideas?? I believe the issue comes down to using an addition / in the ?page= variable. htaccess doesnt seem to like that.


回答1:


After some playing, I figured it out. My regex was off.

Changed:

# Make URLs sexy!
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]

to:

RewriteRule ^(.*)(/)/?$ index.php?page=$1 [L]


来源:https://stackoverflow.com/questions/34424678/htaccess-pretty-url-with-php-url-variables

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