doumengyin0491 2017-02-02 12:24
浏览 64

url重写:从选择框html的值中添加url中的虚拟文件夹

With a select box of towns , i must change the url like that : mywebsite.com/index.php to mywebsite.com/myselecttownvalue/index.php or mywebsite.com/anotherselecttownvalue/index.php

It's possible with an htaccess to manage that ?

Ty for help !

  • 写回答

1条回答 默认 最新

  • dongxun1244 2017-02-03 09:53
    关注

    Certainly it is possible to "accept" incoming URLs, that would mean to rewrite them to an internal, existing route which can process the request and respond to it.

    Concerning your examples: typically one would leave out the script name, so the URLs would look clean, something like https://example.com/town/myselectedtown and get internally rewritten to something like /index.php?town=myselectedtown so that the selected value is available in the $_GET superglobal variable as $_GET['town']. Take care to URL-encode the town name though, in case it contains blanks or funny stuff...

    Take a look at this example setup:

    RewriteEngine on
    RewriteRule ^/?town/(.+)$ /index.php?town=$1 [L]
    

    The above rule will work in dynamic configuration files (".htaccess") and in the real http servers host configuration.

    You certainly could define your URLs without the /town token inside, s you suggested (so just https://example.com/myselectedtown). But that typically raises issues with other URL patterns, since it is never clear if a URL refers to a town name or something else...

    And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line