How to rewrite with .htaccess

跟風遠走 提交于 2019-12-11 11:06:32

问题


I'm pretty new to .htaccess and have a question..

How would I rewrite

http://example.com/forums/view.php?v=Topic%20Name&a=64 to http://example.com/forums/view/Topic-Name/64?

as stated above, I'm new to this..

Also, how would I replace - with   while keeping normal dashes?

Example: Hi There-Bye! would then convert into Hi-There-Bye!

Which would then convert back to Hi There-Bye! with php and .htaccess?


回答1:


The first question is pretty basic. You need an external redirect from the 'ugly' (but working) url to the seo url and an internal rewrite from the seo url to the 'ugly' url. They can all be written in the following format:

#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /forums/view\.php\?v=([^&]+)&a=(.+)\ HTTP
RewriteRule ^ /forums/view/%1/%2 [R,L]
#Change above [R,L] to [R=301,L] after testing all rules work as expected

#Internal rewrite
RewriteRule ^forums/view/([^/]+)/([^/]+)/?$ /forums/view.php?v=$1&a=$2 [L]

The second thing is not possible. There is no way to distinguish between the dash between Hi and There and the dash between There and Bye. I would say: Don't bother. It's a seo-url. If you want to have fancy seo-url's, you'll have to alter your database to contain those fancy seo-urls.




回答2:


Rewrite using below method.

RewriteRule ^example/(.*)\.html$ example/test.php?param=$1



回答3:


RewriteRule ^forums/view/([^/]*)/([^/]*)$ /forums/view.php?v=$1&a=$2 [L]


来源:https://stackoverflow.com/questions/20882185/how-to-rewrite-with-htaccess

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