How to rewrite URL by Htaccess with basic parameters in core PHP

人走茶凉 提交于 2020-02-25 04:22:06

问题


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

my current URL is like this

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.

Desired URL

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

i know that i can do this by htaccess for sure

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 "*"

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.


回答1:


You can use these rules:

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 "*" 


来源:https://stackoverflow.com/questions/60339372/how-to-rewrite-url-by-htaccess-with-basic-parameters-in-core-php

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