简体   繁体   中英

.htaccess how to display long sub-directory urls as short urls in address bar?

I've been trying to solve this problem for a couple of days now. My root folder is https://www.example.com , I have many articles on it but in different folders and sub-folders for better organizing.

The problem is that I want links to look better and not like:

> https://www.example.com/reviews/review_1/index.html
                                 /review_2/index.html
                                 /review_3/index.html

or

https://www.example.com/articles/blog/content_1/index.html
                                     /content_2/index.html
                                     /content_3/index.html

but instead, I want all folders under "reviews and articles" to appear like root/dirs:

https://www.example.com/review_1/index.html
https://www.example.com/content_1/index.html

...so is it possible to exclude ../reviews/ or ../articles/blog/ folder somehow, but to have the same directory structure and also not to have duplicate content, indexes or duplicate DIRs?

EDIT / I found the solution:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^review_1/(.*) reviews/review_1/$1

This code will redirect from folder reviews/review_1/ to unexisting folder review_1 on root and copy all content from reviews/review_1/ to review_1 .

just create an associative arrow specifying the mapping between url's and what they mean under the hood, for example I use in php like this-

$route = array(
   array(
    'url' => 'review_1/index.html',
    'map_to' => '/articles/blog/reviews/index.html'
  ),
   array(
  )
);

or better version like implementing simple routing-

$route = array( array( 'url' => '/review/', 'controller' => 'controller_class', 'action' => 'model_class' ), array( ) );

better proceed with second process.. do some research in implementing routing yourself or use basic framework. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM