简体   繁体   English

如何使用htaccess只重写某些页面

[英]How to use htaccess to rewrite only certain pages

I have a small website I would like to rewrite only some pages with htaccess and hide index.php . 我有一个小型网站,我只想使用htaccess重写某些页面并隐藏index.php。 I was tring this code: 我正在尝试这段代码:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS_HOST} !^$
RewriteCond %{HTTPS_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule    ^despre-noi/?$    despre-noi.php    [R=301,NC,L]    # Handle requests for "despre-noi"
RewriteRule    ^detalii-plati/?$    cum-platesc.php    [R=301,NC,L]    # Handle requests for "modalitati de plata"
RewriteRule    ^contact/?$    contact.php    [R=301,NC,L]    # Handle requests for "contact"
RewriteRule    ^politica-de-confidentialitate/?$    politica-de-confidentialitate.php    [R=301,NC,L]    # Handle requests for "politica de confidentialitate"
RewriteRule    ^termeni-si-conditii/?$    termeni-si-conditii.php    [R=301,NC,L]    # Handle requests for "termeni si conditii"


</ifModule>

The pages are loaded fine but is still showing the .php extension, but if I type in browser the URL without .php extension is whowing without extension. 页面加载良好,但仍显示.php扩展名,但是如果我在浏览器中输入,则不带.php扩展名的URL就是没有扩展名的URL。 could you help to tell me where is wrong? 您能告诉我哪里出问题了吗? Because I want only those pages to be in this condition, not the whole website. 因为我只希望这些页面处于这种状态,而不是整个网站。

Thank You 谢谢

//Edit: I have ssl and for that is that condition with https. //编辑:我有ssl,这就是使用https的条件。

Remove [R=301] from all of your .php rules like 从所有.php规则中删除[R=301]

RewriteRule    ^despre-noi/?$    despre-noi.php    [NC,L]

Add the following slightly modified rule that you had used earlier for all php files. 添加以下您先前对所有php文件使用的稍加修改的规则。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(despre-noi|cum-platesc|contact)\.php [NC]
RewriteRule ^ /%1 [L,R=301]

This needs to come before the .php file rules already in your htaccess. 这需要在您的htaccess中已有.php文件规则之前进行。 Add the names of all the php files you want to work this way inside the expression (..|..) for %{THE_REQUEST} condition. %{THE_REQUEST}条件的表达式(..|..)内添加要以这种方式工作的所有php文件的名称。


domain.com/contact.php to show it domain.com/contact-us domain.com/contact.php以显示它domain.com/contact-us

Yes, it's possible but since the names differ now, you'll have to remove contact from the current %{THE_REQUEST} rule's (..|..) entries and add a separate rule as follows (again before the .php rules) 是的,有可能,但是由于名称现在有所不同,因此您必须从当前%{THE_REQUEST}规则(..|..)条目中删除contact ,并按如下方式添加单独的规则(同样在.php规则之前)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /contact\.php [NC]
RewriteRule ^ /contact-us [L,R=301]

Then just modify the existing .php rule as 然后只需将现有的.php规则修改为

RewriteRule    ^contact-us/?$    contact.php    [NC,L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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