简体   繁体   中英

Seo friendly URL - .htaccess

In the root folder of my hosting I have an htaccess file with, among other things, the following code which redirect to https and www.:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

In the /news/ folder i have entry.php file which have for example?slug=this-is-first-entry. I want it to look like this https://www.example.com/news/this-is-first-entry .

So I want redirect this https://www.example.com/news/entry.php?slug=this-is-first-entry to this https://www.example.com/news/this-is-first-entry

I have this code in .htaccess in /news/ folder:

RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]

It's working fine, but redirecting https and www from root folder does not work. Please help, I am not familiar with htaccess.

This is a THUMB rule. If current directory has a .htaccess with RewriteEngine ON then it overrides parent .htaccess directives. If you want parent .htaccess rules to be applied before then use following option:

RewriteOptions InheritBefore

Before RewriteEngine On line.

So your htaccess file inside /news/ folder will become like:

RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,L]

Additional suggestion: In case you are trying to rewrite non existing files and directories if this is the case then have your htaccess in your news folder like as follows.

RewriteOptions InheritBefore
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)$ entry.php?slug=$1 [NC,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