Home > Software engineering >  Long URL in php using htaccess
Long URL in php using htaccess

Time:07-20

I have a very long URL: //domain/administration/registrar/term_detail.php?a_id=47 And I want to make it looks like: //domain/administration/registrar/detail/47

I have created an htaccess file in //domain/administration/registrar/. Below is my code in htaccess and it is not working. Any suggestion of what I did wrong is appreciated.

Both of my htaccess and term_detail.php files are inside of registrar folder.

RewriteEngine On
RewriteBase /administration/registrar/

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

RewriteCond %{THE_REQUEST} \s/ registrar/detail\.php\?a_id=([^\s&] ) [NC]
RewriteRule ^ administration/registrar/%1? [R=301,L]

RewriteRule ^administration/registrar/([a-zA-Z0-9] )/?$ detail?a_id=$1 [L,QSA,NC]

CodePudding user response:

You can try these rules inside your /administration/registrar/.htaccess:

RewriteEngine On
RewriteBase /administration/registrar/

## External redirect to /administration/registrar/term_detail/47
RewriteCond %{THE_REQUEST} /([^./] )(?:\.php)?\?a_id=([^&\s] )\s [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]

## Internal rewrite to term_detail.php?a_id=47
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-] )/([\w-] )/?$ $1.php?a_id=$2 [QSA,L]

CodePudding user response:

With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

Keep your htaccess file inside registrar folder.

RewriteEngine ON
##External redirect to //domain/administration/registrar/detail/47 URL.
RewriteBase /administration/registrar/
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^/]*)/(?:[^_]*)_([^.]*)\.php\?a_id=(\S )\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]

##Internal rewrite to term_detail.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2/term_$3.php?a_id=$4 [QSA,L]
  • Related