简体   繁体   English

使用 .htaccess 重定向和重写 url

[英]redirect and rewrite url using .htaccess

Recently I was thinking of making some seo friendly urls by rewrite and redirecting urls.最近我在考虑通过重写和重定向 url 来制作一些 seo 友好的 url。

the rule which I wrote is working fine for this type of url like xyz.com/abc.php this automatically got converted into xyz.com/abc now I need help with this type of urls abc.com/study.php/abc/123 which I want to redirect on abc.com/study/abc/123我写的规则适用于像xyz.com/abc.php这样的类型的 url 这会自动转换为xyz.com/abc现在我需要这种类型的 url 的帮助abc.com/study.php/abc/123我想在abc.com/study/abc/123上重定向

my .htaccess file is我的 .htaccess 文件是

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

EDIT .编辑

It was working fine now I have changed my mind that instead of removing .php extension from all files I want to remove .php extension specifically from 2 files ie a.php and b.php .它现在工作正常,我改变了主意,我不想从所有文件中删除 .php 扩展名,而是想从 2 个文件(即a.phpb.php删除 .php 扩展名。

You can tweak your existing rules to support this PATH_INFO as well like this:您可以调整现有规则以支持此PATH_INFO以及如下所示:

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+(\S+?)\.php(/\S*)?\sHTTP [NC]
RewriteRule ^ /%1%2 [L,R=301,NE]

# check to see if the request is for a PHP file:
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.+?)(/.*)?$ /$1.php$2 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM