Make a friendly URL with 2 parameters on PHP with Apache

谁都会走 提交于 2019-12-11 06:39:58

问题


So I am in a bit of a soup here.

I need to make a friendly URL.

This is the URL where I enter the search values project_name and version

localhost:8080/demo_webpages_project/retrieval.html

This is where it takes me to on submitting the values

localhost:8080/demo_webpages_project/download.php?project_name=QT&version=3.1.2

I want the URL in the following form

localhost:8080/demo_webpages_project/download/QT/3.1.2

I tried doing this using .htaccess but not really finding a solution

The following is the code for the .htaccess file.

<IfModule mod_rewrite.c>
#Turn the engine on
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#attempt1
#RewriteRule ^(.*)/$ download.php?project_name=$1&version=$1
#RewriteRule ^/(.*)$ download.php?version=$1

#attempt2
#RewriteRule ^(.*)/(.*)$ download.php?project_name=$1&version=$2 [L,NC,QSA]
#RewriteRule ^(.*)$ download.php?version=$1

</IfModule>

I would be really grateful if someone could help me out with this. This is the first time I am working on something on this and am lost.

Thanks


回答1:


Have this .htaccess inside /demo_webpages_project/:

Options -MultiViews
RewriteEngine on
RewriteBase /demo_webpages_project/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ download.php?project_name=$1&version=$2 [L,QSA]



回答2:


According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:

RewriteEngine on 
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

And this maps requests from

/news.php?news_id=63

to

/news/63.html

Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:

<Files news>
    ForceType application/x-httpd-php
</Files>

And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:

<?php
    echo $_SERVER['PATH_INFO'];
    // outputs '/63.html'
?>


来源:https://stackoverflow.com/questions/39623536/make-a-friendly-url-with-2-parameters-on-php-with-apache

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