Htaccess url rewrite to .php

I have the following url

http://www.***.org/aaddress.php?state=nm&office_id=852&office_name=CLOVIS&state_name=New%20Mexico 

I want to change it below.

 http://www.*****.org/aaddress-state-nm-office_id-852-office_name-CLOVIS-state_New%20Mexico.php 

I used below htaccess code

 Options +FollowSymLinks RewriteEngine on RewriteRule aaddress-state-(.*)-office_id-(.*)-office_name-(.*)-state_name-(.*)\.php aaddress.php?state=$1&office_id=$2&office_name=$3&state_name=$4 

It does not work, when I press the record button, the address did not translate to the .php page

My links inside the pages as the 1st link and you want to change it as a .php link

+4
source share
3 answers

use it

 RewriteRule ^aaddress-state-([^-]+)-office_id-([^-]+)-office_name-([^-]+)-state_name-([^\.]+)\.php$ aaddress.php?state=$1&office_id=$2&office_name=$3&state_name=$4 

^ and $ will match the entire URL, and [^-]+ will match anything if it doesn't occur - . And [^\.]+ Will match anything if not encountered . .

This will match aaddress-state-nm-office_id-852-office_name-CLOVIS-state_name-New Mexico.php but not aaddress-state-nm-office_id-852-office_name-CLOVIS-state_New Mexico.php . Notice the missing part of name- in state_New Mexico at a later URL.

+1
source

Try the following:

 RewriteRule ^aaddress-state-(.*)-office_id-(.*)-office_name-(.*)-state_name-(.*)\.php aaddress.php?state=$1&office_id=$2&office_name=$3&state_name=$4 [QSA,L] 
0
source

This is a general recursion-based solution that works no matter how long your query string is.

 Options +FollowSymLinks -MultiViews RewriteEngine on RewriteBase / RewriteRule ^aaddress\.php$ aaddress-%{QUERY_STRING} [L] RewriteRule ^([^&]+)&([^&]+)(&.*)?$ $1-$2$3 [L] RewriteRule ^([^&])& $1 [L] RewriteRule ^([^=]+)=([^=]+)(=.*)?$ $1-$2$3 [L] RewriteRule ^([^=])= $1 [L] 

This will internally redirect your approximate URL: /aaddress-state-nm-office_id-852-office_name-CLOVIS-state_name-New+Mexico , replacing each & = line of the query string with a hyphen - .

0
source

Source: https://habr.com/ru/post/1394562/


All Articles