简体   繁体   中英

Rewrite rule in .htaccess

I want to redirect an URL from: http://some-url/pl/news/ to http://some-url/blog . but when user type also http://some-url/news/ (without /pl) a want to redirect it also to: http://some-url/blog . Because of that I wrote a rule:

RewriteRule (pl)?/news(.*)  /blog/ [R=301,L]

When I test it in regex tester it matches in both cases either with /pl or without it. but when I try to test this rule in .htaccess tester it only matches when there is an URL with /pl . Could someone explain me that, why does it happen? And what is the right way to write this rule in proper way?

Using htaccess in your root level of your web server, how you redirect one page to another is:

RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]

Or

Redirect 301 /path/to-old-url    http://www.cyourdomain.com/path/to-new-url

To redirect the contents of a whole directory to another use the below:

RewriteRule ^subdirectory/(.*)$ /anotherdirectory/$1 [R=301,NC,L]

To redirect the contents of a whole directory to the webserving root:

RewriteRule ^subdirectory/(.*)$ /$1 [R=301,NC,L]

To redirect the contents of a subdirectory to another domain but in the same subdirectory

 Redirect 301 /subdirectory http://www.anotherdomain.com/subdirectory

Make sure that the opening of the .htaccess file contains the 2 lines of code below which enables the Apache module to rewrite the URLS, then place your redirections below them

Options +FollowSymLinks
RewriteEngine On

Redirect URLs From Old To New Domain Using 301 Redirects In .htaccess

When you need to switch a website from an old domain to a new domain, you need to redirect all your page URLs, this is when htaccess is your friend.

The code below will create 301 url redirects for both the www and non-www version of 'olddomain.com' to the new domain 'newdomain.com'.

Add this .htaccess file to the OLD site webroot and upload the files from the old site to the new to see a seamless switch from an old domain to a new one.

So the example below is redirecting all URLs from olddomain.com to newdomain.com

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

You can also apply this to a subdomain – so the example below is redirecting all URLs from subdomain.olddomain.com to subdomain.newdomain.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$ 
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L] 
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$ 
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]

To force a website to use the secure protocol SSL running the whole site over HTTPS you can make a simple edit to the .htaccess file in the document root.

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

To force a particular folder or directory to serve over SSL, create a .htaccess file in that folder and apply the following to it:

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]

This assumes SSL is enabled on the domain on an Apache Web Server with the mod_rewrite module enabled.

I'm not 100% sure why that happens, so my suggestion is to try putting this in your .htaccess file and see if that works.

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} /pl/news(.*) [OR]
RewriteCond %{REQUEST_URI} /news(.*)
RewriteRule .* /blog [R=301]

尝试像这样的事情

RewriteRule ^(pl/)?news/?$ /blog/ [R=301,L]

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