Rename Url .htaccess

你说的曾经没有我的故事 提交于 2019-12-25 03:38:08

问题


I'm having trouble with mod-rewrite to rewrite these url:

mywebsite.com/index.php?page=folder/file
mywebsite.com/index.php?page=folder/file&id=10

into these

mywebsite.com/file
mywebsite.com/file/10

How can I do that?


回答1:


mywebsite.com/index.php?page=folder/file
mywebsite.com/index.php?page=folder/file&id=10
into these
mywebsite.com/file
mywebsite.com/file/10

It is not clear if folder is a fixed string or the directory where file is, so in this answer it is assumed to be a fixed string as the directory in both cases is root.

You may try this in one .htaccess file at root directory:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  !index\.php  [NC]
RewriteRule  ^([^/]+)/?$  /index.php?page=folder/$1 [L,NC]

RewriteCond %{REQUEST_URI}  !index\.php  [NC]
RewriteRule  ^([^/]+)/([^/]+)/? /index.php?page=folder/$1&id=$2  [L,NC]

Maps silently:

http://mywebsite.com/file to
http://mywebsite.com/index.php?page=folder/file

And

http://mywebsite.com/file/10 to
http://mywebsite.com/index.php?page=folder/file&id=10

Where strings file and 10 are assumed to be dynamic.

For permanent redirection, replace [L,NC] with [R=301,L,NC]




回答2:


You want somthing similar to this

#With mod_rewrite
RewriteEngine on
RewriteRule   ^/file/(.+)  /index.php?page=folder/file&id=$1 [R,L]
RewriteRule   ^/file       /index.php?page=folder/file [R,L]



回答3:


Do you mean you want to rewrite and NOT TO REDIRECT mywebsite.com/$var into mywebsite.com/index.php?page=folder/$var and mywebsite.com/$var1/$var2 into mywebsite.com/index.php?page=folder/$var1&id=$var2? You can check this:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^([a-z0-9-_]+)$ /index.php?page=folder/$1

RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)$ /index.php?page=folder/$1&id=$2


来源:https://stackoverflow.com/questions/15777229/rename-url-htaccess

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