Dynamic url in php without filename

早过忘川 提交于 2019-12-12 02:51:04

问题


I couldn't find exact answer to my question so I am posting this. Maybe it involves htaccess configuring.

What I would like is to have http://www.example.com/static_folder/dynamic_parameter

the above url should run the index.php in static_folder and I should be able to get that dynamic_parameter and output accordingly.

How to achieve this?

Thanks.


回答1:


A littlebit of htaccess can do it pretty easily. As for example, save this rule in the root directory's htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/static_folder/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /static_folder/index.php [L]
</IfModule>

Then use $_SERVER['REQUEST_URI'] inside /static_folder/index.php to get the dynamic_parameter. Is it what you are trying to achieve?




回答2:


Try this in your .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

then you can do:

sudo a2enmod rewrite

sudo service apache2 restart

Also check answers for this question: How to remove all .php extension from a dir and all of its subdirectories?




回答3:


I believe the OP asked for index.php inside static_folder to get the variable, so, something like this, should do the trick:

RewriteRule ^static_folder/([^/]+)$ static_folder/index.php?variable=1$ [NC,L]


来源:https://stackoverflow.com/questions/25176270/dynamic-url-in-php-without-filename

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