简体   繁体   中英

How can I redirect multiple URLs to a single URL for the homepage?

I have a site built on codeigniter but I have come accross a small issue. Basically the homepage can be accessed through 3 different urls, being:

www.domain.com/en

www.domain.com/en/

www.domain.com/en/home.html

I'd like the first and third url to redirect to the second (www.domain.com/en/).

I've been playing around with .htaccess for hours and I can't seem to get any changes done.

Any help would be really appreciated.

Edit: Here is my current .htaccess

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^mywebsite.com [NC] 
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]

RewriteRule ^en(/home.html)?$ /en/ [L]

RewriteRule ^en(/home.html)?$ /en/ [L]

这仅匹配/ en和/en/home.html,不匹配其他网址。

Your current solution doesnt work because any input has allready been captured by RewriteRule ^(.*)$ /index.php/$1 [QSA,L] The L flag denotes last, meaning that no further rules should be processed.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

#Everything from here has to do with the line mentioned above
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L] # This catches all input

RewriteCond %{HTTP_HOST} ^mywebsite.com [NC] 
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]

RewriteRule ^en(/home.html)?$ /en/ [L]

Instead, anything you want processed needs to be moved up before that line:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /


RewriteCond %{HTTP_HOST} ^mywebsite.com [NC] 
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]

RewriteRule ^en(/home.html)?$ /en/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

Beyond that, reading up on mod_rewrite and regular expressions cant hurt here.

Could be as simple as changing

^en(/home.html)?$ /en/ [L]

to

^/en(/home.html)?$ /en/ [L] (notice the extra slash)

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