Hide Part of URL htaccess

半世苍凉 提交于 2019-12-24 09:28:18

问题


The Goal:

  1. The user lands on the site where the latest 10 articles are shown (no problem here).
  2. The user clicks on the title of one article using this href link in a table:

    <a href='"."article.php/".$type."/".$id."/".$web_title."'>".$title."</a>

  3. All links from any $type get directed to article.php.
  4. article.php should be built dynamically based on the $id retrieving information from the database.

I've got this URL rewrite in my htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/article/?
RewriteCond %{REQUEST_FILE} !-f
RewriteCond %{REQUEST_FILE} !-l
RewriteRule ^(.*)$ article.php?post_type=$1&post_id=$2&post_name=$3 [QSA,L]

Which is producing the URL: http://example.org/article.php/news/1/first-article-test when the user accesses article.php via a link described in point 2.

I'm trying to remove the article.php section from the above URL, so it looks just like this: http://example.org/news/1/first-article-test

At the moment the article.php page is receiving the variables just fine, it's pulling down other data from the database based on the $id passed by the link, but the URL is displaying article.php/....


回答1:


Keep your rule like this:

RewriteEngine On

RewriteCond %{REQUEST_FILE} !-f
RewriteCond %{REQUEST_FILE} !-l
RewriteCond %{REQUEST_URI} !^/article\.php [NC]
RewriteRule ^(.+)$ /article.php/$1 [L]

It will let you have your links as

http://example.org/news/1/first-article-test


来源:https://stackoverflow.com/questions/24041964/hide-part-of-url-htaccess

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