dtz46697 2012-11-28 19:51
浏览 48
已采纳

使用Apache mod_rewrite时,<a>标签中href属性的正确形式是什么?

I want to make my URL clean. Below is what I want to do:
Dirty URL: www.site.com/index.php?page=products
Clean URL: www.site.com/products/

For this, I write these codes in .htaccess file:

Options +FollowSymLinks
RewriteEngine On 
RewriteBase /

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

But whenever I click on the links in my project, I redirect to localhost page. Links are in this form:

<a href="products">Products</a>

I think it may be related to href value.
What's the correct form for href value? href="product",href="/product/",etc. ?

  • 写回答

1条回答 默认 最新

  • dongquanlin1885 2012-11-28 20:04
    关注

    There is no correct form for your href attribute, each form acts in different ways:

    <a href="products">Products</a>
    

    The above would go relatively from the page you are currently on, so might be domain.com/products but might be domain.com/shop/products

    You are better using a link direct from the root by using a / at the start, for example:

    <a href="/products">Products</a>
    

    would go to domain.com/products (note the lack of a / at the end of the url, which will make it not work with your current htaccess rule)

    To make both /products and /products/ go to the correct place, change your htaccess rule to:

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

    The ? will make the character preceding it optional, so the / in this case.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)