空查询附加到主URL .htaccess问题

时间:2022-07-15 10:42:41

I am using .htaccess to clean up my URL. It works fine generally when clicking on different links like www.example.com/el-nino-effect. However when I go to www.example.com explicitly, it takes me to www.example.com/index?iden= rather than just www.example.com. Though they are the same page, this primary URL somehow screws up. Can you help?

我正在使用.htaccess清理我的网址。点击www.example.com/el-nino-effect等不同的链接时,它通常可以正常工作。但是,当我明确访问www.example.com时,它会转到www.example.com/index?iden=而不仅仅是www.example.com。虽然它们是同一页面,但这个主要URL在某种程度上搞砸了。你能帮我吗?

The 4th para is where the cleaner URL code is present in the .htaccess, but I'm posting the whole file nonetheless. Also funnily, this problem is not happening in Chrome Browser on Ubuntu, but is happening on the Chrome browser on a chromebook.

第4段是.htaccess中存在更清晰的URL代码的地方,但我仍在发布整个文件。同样有趣的是,这个问题在Ubuntu上的Chrome浏览器中没有发生,但是在Chromebook上的Chrome浏览器上发生了。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

# For cleaner URLs making ?q=el-nino to /el-nino
RewriteRule ^([^/\.]+)?$ index.php?iden=$1 [L]
RewriteRule ^([^/\.]+)/?$ index.php?iden=$1 [L]
# RewriteRule ^downloads/([^/\.]+)/?$ downloads.php?id=$1 [L]

# For rewriting to HTTPS
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

1 个解决方案

#1


1  

Actually your rules look fine but they are not in right order. In general keep redirect rules before internal rewrites like this:

实际上你的规则看起来很好,但它们的顺序不正确。通常在内部重写之前保留重定向规则,如下所示:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For rewriting to HTTPS
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# For cleaner URLs making ?q=el-nino to /el-nino
RewriteRule ^([^.]+)/?$ index.php?iden=$1 [L,QSA]
# RewriteRule ^downloads/([^/\.]+)/?$ downloads.php?id=$1 [L,QSA]

Make sure to test this after clearing your browser cache.

确保在清除浏览器缓存后进行测试。

#1


1  

Actually your rules look fine but they are not in right order. In general keep redirect rules before internal rewrites like this:

实际上你的规则看起来很好,但它们的顺序不正确。通常在内部重写之前保留重定向规则,如下所示:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For rewriting to HTTPS
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# For cleaner URLs making ?q=el-nino to /el-nino
RewriteRule ^([^.]+)/?$ index.php?iden=$1 [L,QSA]
# RewriteRule ^downloads/([^/\.]+)/?$ downloads.php?id=$1 [L,QSA]

Make sure to test this after clearing your browser cache.

确保在清除浏览器缓存后进行测试。