htaccess的重定向友好的URL [英] htaccess redirect for friendly URL

查看:112
本文介绍了htaccess的重定向友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有行之有效.htaccess文件。到目前为止,我有:

I currently have a .htaccess file that works well. So far I have:

Options +FollowSymlinks -Multiviews
#first part
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mydomain.net$ [NC]
RewriteRule ^(.*)$ http://mydomain.net/$1 [R=301,L]

#second part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$ /$1/$2/$3.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

#third part
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

在第一行是需要我,因为我使用GoDaddy的托管。 第一部分是重定向www.mydomain.net到mydomain.net。 第二部分是重定向mydomain.net/abcd到mydomain.net/abcd.php~~V。 第三部分是在URL的末尾摆脱.PHP的。

The very first line is needed for me because I use GoDaddy hosting. The first part is redirecting www.mydomain.net to mydomain.net. Second part is redirecting mydomain.net/abcd to mydomain.net/abcd.php. Third part is getting rid of .php at the end of URL.

现在我需要添加codeS,将重定向mydomain.net/products/?category=electronics到mydomain.net/products/electronics,但我不能让它的工作。

Now I need to add codes that will redirect mydomain.net/products/?category=electronics to mydomain.net/products/electronics, but I can't get it to work.

我已经尝试添加重写规则/products/(.*)$ /产品/?类别= $ 1等众多codeS,我发现对堆栈溢出,但没有人似乎为我工作。

I've tried adding "RewriteRule /products/(.*)$ /products/?category=$1" and many other codes that I found on Stack Overflow, but none of them seems to work for me.

感谢你们的时间。

推荐答案

让您的code是这样的:

Have your code like this:

Options +FollowSymlinks -Multiviews

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.mydomain.net$ [NC]
RewriteRule ^(.*)$ http://mydomain.net/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

RewriteCond %{THE_REQUEST} /(products)/\?category=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^(products)/([^/]+)/?$ /$1/?category=$2 [L,NC,QSA]

RewriteRule ^([^/]+)/([^/]+)/?$ /$1/$2.php [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1/$2/$3.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

这篇关于htaccess的重定向友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆