如何通过Htaccess用核心PHP中的基本参数重写URL [英] How to rewrite URL by Htaccess with basic parameters in core PHP

查看:78
本文介绍了如何通过Htaccess用核心PHP中的基本参数重写URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对此URL重写有疑问。

Hello all i am having a issue with this URL rewrite.

我当前的URL就是这样


domain.com/professional?seo=ACCOUNTIFY-SERVICES-&-ADVISORS&id=3173

domain.com/professional?seo=ACCOUNTIFY-SERVICES-&-ADVISORS&id=3173

这里的页面名称专家可以更改为任何页面,例如我将使用其他页面名称的类似网址。

here the page name professional can be changed to any page like i will have the similer url with other page names.

所需的URL


domain.com/professional/ACCOUNTIFY-SERVICES-&-ADVISORS/3173

domain.com/professional/ACCOUNTIFY-SERVICES-&-ADVISORS/3173

我知道我可以通过htaccess做到这一点

这是我的htaccess文件

here is my htaccess file

RewriteEngine On
RewriteBase /
Options All -Indexes
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>

RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

# remove index.php
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Header set Access-Control-Allow-Origin "*"

请建议我在此处进行更改以实现所需的url。
非常感谢您的帮助。
i在这里一直在阅读一些答案,如果我找到解决方案,我会在这里发布。

please suggest me the changes needs to be done here to achieve the desired url. Thank you so much for any help. i have been reading some answers here and if i find a solution i will post here.

推荐答案

您可以使用这些规则:

RewriteEngine On
RewriteBase /
Options All -Indexes

RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule ^ - [L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([^.]+)(?:.php)?\?seo=([^\s&]+)&cat=([^\s&]+)&id=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L,NE]

RewriteCond %{THE_REQUEST} \s/+([^.]+)(?:.php)?\?seo=([^\s&]+)&id=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L,NE]

# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]

# internal forward from pretty URL to actual one
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?&seo=$2&cat=$3&id=$4 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/([^/]+)/([^/]+)/?$ $1.php?&seo=$2&id=$3 [L,QSA]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Header set Access-Control-Allow-Origin "*" 

这篇关于如何通过Htaccess用核心PHP中的基本参数重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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