.htaccess basic mod rewrite

假装没事ソ 提交于 2020-01-04 04:10:55

问题


I am only just starting to learn how to rewrite urls with the .htaccess file.

How would I change:

http://www.url.net/games/game_one.php

into this:

http://www.url.net/games/game-one/

This is what I have been trying

RewriteRule    ^/games/game-one.php    ^/games/game-one/    [NC,L]

回答1:


If you want people to use /games/game-one/ explicitly, you have to rewrite so that it requests /game/game-one.php. So the opposite way around than you have it in your question.

RewriteEngine On
RewriteRule ^games/game-one/$ /games/game-one.php

If you want to rewrite other URL's too, then you'd need to use a technique similar to the prior answer.




回答2:


Try this:

RewriteRule ^(/games/game-one)\.php $1/

What that says is match anything starting with /games/game-one and remember the first part of that match, then replace it with the first part (capturing group in regex speak), and a slash character. Note that to match a period character you must precede it with a \ since . is a special character that means "any character" (at least if you care to avoid matching any character).



来源:https://stackoverflow.com/questions/15490293/htaccess-basic-mod-rewrite

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