Home > Software design >  Htaccess rule for pretty url in sub folder html file
Htaccess rule for pretty url in sub folder html file

Time:08-04

I am currently trying to make pretty url and I copied some code from other post and was successful to rewrite using below code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?g/(.*?)/?$ /g.html?name=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /g\.html\?name=([^\&\ ] )
RewriteRule ^/?g\.html$ /g/%1? [L,R=301]
RewriteCond %{THE_REQUEST} /([^.] )\.html [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]

It changes my main folder g.html?name=this into g/this but I need to rewrite html file in my sub folder which is /sub/w.html plz help to make it aslo sub/w/this

CodePudding user response:

Place these rules at top of your htaccess rules file and try checking your URLs. Please make sure to clear your browser cache before testing your URLs.

Also make sure to keep your htaccess file along with new folder(not inside it, along with it).

RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(new)/w\?name=(\S )\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(name)/([^/]*)/?$ $1/$2.html [L]

NOTE: I am matching specifically matching name folder here in Rule because your already existing rules also have checks for non-existing pages, so didn't want to conflict it.

  • Related