間違ったURLが入力されたときに404ページにリダイレクトする

Jul 23 2020

404エラーページがあり、このリンクから生成された以下のコードを試しました。ただし、間違ったURLが入力されると、404エラーページではなくメインページにリダイレクトされます。

<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 https://www.rcis.in/404.php
</IfModule>

htaccessファイルにコードを追加しました。また、URLからファイル拡張子を削除する方法。以下は私が試したコードです

 #Remove extension
 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^\.]+)$ $1.php [NC,L]

そして、私は上記のそのウェブサイトから生成された以下のコードを試しました

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?(.*).(php)$ /$1 [R=301,L]

</IfModule>

どうすればこれら2つを解決できますか

前もって感謝します

回答

2 Ajith Jul 23 2020 at 13:53

以下のコードは、URLからphp拡張を削除します

RewriteEngine On
# for 404 redirection
ErrorDocument 404 https://www.yourdomain.in/404
# below code rewrites php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# Remove php extention
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

したがって、URL https://www.yourdomain.in/404 ロードされます https://www.yourdomain.in/404.php

fidaay Jul 26 2020 at 06:41

ここに私の完全な.htaccessファイルを残します:

#turn on url rewriting 
RewriteEngine on

# below code rewrites php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

# Remove php extention
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

#redireccion de errores 
ErrorDocument 403 403.php #your 403 page
ErrorDocument 404 404.php #your 404 page

# Setup browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>

# Desactivando el file directory
Options -Indexes

次のコードの使用に注意してください。

# QUE ES ESTO??
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

それはあなたの間違ったURLリダイレクトを破壊します。

これも役立つ場合があり、httpsの使用を保護します。

# Redireccion total a Https
<IfModule mod_rewrite.c>
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
</IfModule>

乾杯、これがあなたを助けることを願っています。