htaccess mod-rewrite ~ how to modify url from /pages/pagename.cfm to /pagename/

别等时光非礼了梦想. 提交于 2019-12-24 12:16:20

问题


I have to do a website structure cleanup on a site... http://dev.514hebergement.com/

looking for the htaccess directive to rewrite all pages url to a friendlier url structure without moving the actual pages in folder hierarchy.

so every page that is within the /pages/pagename.cfm

would rewrite its address to /pagename/

and respond to both /pagename/ and /pages/pagename.cfm urls.

lets keep in mind I need the pagename to be dynamic, not hardcoded in the htaccess directive.

Thanks a lot!

my current htaccess is situated in the root of the website:

Options +FollowSymLinks
RewriteEngine on    

# DROP THE INDEX FILENAME #
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.cfm($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)INDEX\.CFM($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)INDEX\.cfm($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.CFM($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

# v3 PAGES #
RedirectPermanent /Hebergement-Internet-Montreal/ /
RedirectPermanent /Hebergement-Web-Montreal/ /
RedirectPermanent /Hebergement-Web-Montreal/commander-hebergement.cfm /commander.cfm
RedirectPermanent /hebergement-php.cfm /forfaits-linux.cfm
RedirectPermanent /Hebergement-Web-Montreal/faq.cfm /faq.cfm
RedirectPermanent /Hebergement-Web-Montreal/termes.cfm /termes.cfm
RedirectPermanent /Hebergement-Internet-Montreal/contact.cfm /contact.cfm

# SOLUTIONS-TECHNOLOGIES #
RedirectPermanent /hebergement-linux.cfm /hebergement-web-linux.cfm
RedirectPermanent /hebergement-coldfusion.cfm /hebergement-web-coldfusion.cfm
RedirectPermanent /hebergement-windows.cfm /hebergement-web-windows.cfm
RedirectPermanent /hebergement-mysql.cfm /hebergement-web-mysql.cfm
RedirectPermanent /hebergement-perl.cfm /hebergement-web-perl.cfm
RedirectPermanent /hebergement-php.cfm /hebergement-web-php.cfm


# ARCHIVED PAGES #
RedirectPermanent /Hebergement-Internet-Montreal/promotions.cfm /
RedirectPermanent /promotions.cfm /
RedirectPermanent /hebergement-asp.cfm /forfaits-windows.cfm

the exact address im entering in the address bar is: http://dev.514hebergement.com/pages/hebergement-web-montreal-qualite.cfm

results in url rewrited to: /pages/hebergement-web-montreal-qualite

I want it like this: /hebergement-web-montreal-qualite/

OK that works... added the trailing slash to the rewrited url ->RewriteRule ^ /%1/ [R=302,L,NE]

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+pages/(.+?)\.cfm[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/pages/$1\.cfm -f [NC]
RewriteRule ^(.+?)/?$ pages/$1.cfm [L]

So the last step would be to redirect the root pages to the /pages/pagename.cfm files...

can that be dynamic or will I have to list all pages independently?


回答1:


You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+pages/(.+?)\.cfm[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/pages/$1\.cfm -f [NC]
RewriteRule ^(.+?)/?$ pages/$1.cfm [L]


来源:https://stackoverflow.com/questions/27553722/htaccess-mod-rewrite-how-to-modify-url-from-pages-pagename-cfm-to-pagename

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