Php 使用htaccess重写url而不重定向
我有这样一个url: 我想将此url更改为: 我不需要url重定向。 我当前的htaccess文件如下所示:Php 使用htaccess重写url而不重定向,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我有这样一个url: 我想将此url更改为: 我不需要url重定向。 我当前的htaccess文件如下所示: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
重新启动发动机
重写基/
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]
.htaccess文件
添加此代码
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
- rewrite ENGINE On启用mod_rewrite
- RewriteCond%{HTTP_HOST}显示我们在重写过程中执行和不执行的URL。
- 在本例中,我们希望匹配example.com
- !!意思是“不”。我们不想重写已经包含folder1的URL,因为这样它会不断添加folder1,它会变成一个无限长的URL
- [NC]匹配URL的大小写版本
- 重写规则定义特定的规则
- 重写规则后的第一个字符串定义了原始URL的外观。在本文的最后,有一个关于特殊字符的更详细的解释
- 重写规则后的第二个字符串定义新URL。这与文档根目录(html)有关表示html目录本身,也可以指定子文件夹
希望这有帮助 您只想将
code\u category
重定向到category
外部,并保持路径内部不变,因此,请尝试以下操作:
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
上述操作将从外部将任何包含code\u category/whatever
的请求重定向到category/whatever
,并保持内部路径不变
如果您只希望请求包含code\u类别/computers/
请将其更改为:
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
测试它,如果正确,将302
更改为301
以实现永久重定向
注意:清除浏览器缓存,然后测试它。这是wordpress还是其他CMS?Hi Shivraj谢谢帮助,如果用户键入url,它应该显示页面内容,我需要什么。在这种情况下,
RewriteRule^/category/computers/$code\u category/computers/
Hi Mohammed,我应该在哪里添加它我已经在上面提到了我的htaccess文件/