.htaccess代码在本地可以正常工作,但不能在cpanel在线上工作

时间:2018-12-02 14:38:39

标签: php .htaccess

我正在使用以下代码制作漂亮的URL

#code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]

此代码在localhost上似乎运行得非常完美,但是一旦我将其在线部署,它就无法工作,也没有显示任何内部错误,表明未找到404。好像是在寻找文件夹或文件一样。

错误出在哪里,或者我缺少在本地主机和在线上应该记住的任何逻辑。

我有一个已重定向到文件的URL

www.xyz.com/technology(此URL仅用于获取类别) www.xyz.com/technology/some-post(此URL仅用于获取类别和条目)

我有一些网址:

www.xyz.com/post.php?category=technology&slug=some-slug 至 www.xyz.com/technology/some-slug(现在这条规则,如果它只有一个参数,那么我将只查询一个类别,如果它有2个参数,那么我将对类别和子类别运行错误)

这是我的整个.htaccess代码

<IfModule mod_rewrite.c>
RewriteEngine On

#for normal domains like example.com or example.in to redirect to https://www.example.com
#RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^([^\.]+)\.(com|in)$ [NC]
#RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L,NE]

#for subdomain like test.example.com to redirect to https://test.example.com
#RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
#RewriteCond %{HTTPS} off
#RewriteCond %{HTTP_HOST} ^([\.\w\-]*)\.(com|in)$ [NC]
#RewriteRule ^ https://%1.%2%{REQUEST_URI} [R=301,L,NE]

# Block suspicious request methods
RewriteCond %{REQUEST_METHOD} ^(HEAD|TRACE|DELETE|TRACK|DEBUG) [NC]
RewriteRule ^(.*)$ - [F,L]

RewriteRule ^post/(\d+)*$ ./post.php?category=$1&slug=$2

# Block WP timthumb hack
RewriteCond %{REQUEST_URI} (timthumb\.php|phpthumb\.php|thumb\.php|thumbs\.php) [NC]
RewriteRule . - [S=1]

# Block suspicious user agents and requests
RewriteCond %{HTTP_USER_AGENT} (libwww-perl|wget|python|nikto|curl|scan|java|winhttp|clshttp|loader) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} (;|<|>|'|"|\)|\(|%0A|%0D|%22|%27|%28|%3C|%3E|%00).*(libwww-perl|wget|python|nikto|curl|scan|java|winhttp|HTTrack|clshttp|archiver|loader|email|harvest|extract|grab|miner) [NC,OR]
RewriteCond %{THE_REQUEST} \?\ HTTP/ [NC,OR]
RewriteCond %{THE_REQUEST} \/\*\ HTTP/ [NC,OR]
RewriteCond %{THE_REQUEST} etc/passwd [NC,OR]
RewriteCond %{THE_REQUEST} cgi-bin [NC,OR]
RewriteCond %{THE_REQUEST} (%0A|%0D) [NC,OR]

# Block MySQL injections, RFI, base64, etc.
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=http:// [OR]
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=(\.\.//?)+ [OR]
RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=/([a-z0-9_.]//?)+ [NC,OR]
RewriteCond %{QUERY_STRING} \=PHP[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} [NC,OR]
RewriteCond %{QUERY_STRING} (\.\./|\.\.) [OR]
RewriteCond %{QUERY_STRING} ftp\: [NC,OR]
RewriteCond %{QUERY_STRING} http\: [NC,OR]
RewriteCond %{QUERY_STRING} https\: [NC,OR]
RewriteCond %{QUERY_STRING} \=\|w\| [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)/self/(.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*)cPath=http://(.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*iframe.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^i]*i)+frame.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [NC,OR]
RewriteCond %{QUERY_STRING} base64_(en|de)code[^(]*\([^)]*\) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
RewriteCond %{QUERY_STRING} (NULL|OUTFILE|LOAD_FILE) [OR]
RewriteCond %{QUERY_STRING} (\./|\../|\.../)+(motd|etc|bin) [NC,OR]
RewriteCond %{QUERY_STRING} (localhost|loopback|127\.0\.0\.1) [NC,OR]
RewriteCond %{QUERY_STRING} (<|>|'|%0A|%0D|%27|%3C|%3E|%00) [NC,OR]
RewriteCond %{QUERY_STRING} concat[^\(]*\( [NC,OR]
RewriteCond %{QUERY_STRING} union([^s]*s)+elect [NC,OR]
RewriteCond %{QUERY_STRING} union([^a]*a)+ll([^s]*s)+elect [NC,OR]
RewriteCond %{QUERY_STRING} (;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*(/\*|union|select|insert|drop|delete|update|cast|create|char|convert|alter|declare|order|script|set|md5|benchmark|encode) [NC,OR]
RewriteCond %{QUERY_STRING} (sp_executesql) [NC]
RewriteRule ^(.*)$ - [F,L]

# We are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php

# Redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]

# Adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]

# Over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

#code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]

# We are setting this rules to stop botnets and hackers or other users from copying website contents i.e antihack

RewriteCond %{HTTP_USER_AGENT} ^BlackWidow [OR]
RewriteCond %{HTTP_USER_AGENT} ^Bot\ mailto:craftbot@yahoo.com [OR]
RewriteCond %{HTTP_USER_AGENT} ^ChinaClaw [OR]
RewriteCond %{HTTP_USER_AGENT} ^Custo [OR]
RewriteCond %{HTTP_USER_AGENT} ^DISCo [OR]
RewriteCond %{HTTP_USER_AGENT} ^Download\ Demon [OR]
RewriteCond %{HTTP_USER_AGENT} ^eCatch [OR]
RewriteCond %{HTTP_USER_AGENT} ^EirGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^EmailWolf [OR]
RewriteCond %{HTTP_USER_AGENT} ^Express\ WebPictures [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro [OR]
RewriteCond %{HTTP_USER_AGENT} ^EyeNetIE [OR]
RewriteCond %{HTTP_USER_AGENT} ^FlashGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetRight [OR]
RewriteCond %{HTTP_USER_AGENT} ^GetWeb! [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go!Zilla [OR]
RewriteCond %{HTTP_USER_AGENT} ^Go-Ahead-Got-It [OR]
RewriteCond %{HTTP_USER_AGENT} ^GrabNet [OR]
RewriteCond %{HTTP_USER_AGENT} ^Grafula [OR]
RewriteCond %{HTTP_USER_AGENT} ^HMView [OR]
RewriteCond %{HTTP_USER_AGENT} HTTrack [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Stripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^Image\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} Indy\ Library [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^InterGET [OR]
RewriteCond %{HTTP_USER_AGENT} ^Internet\ Ninja [OR]
RewriteCond %{HTTP_USER_AGENT} ^JetCar [OR]
RewriteCond %{HTTP_USER_AGENT} ^JOC\ Web\ Spider [OR]
RewriteCond %{HTTP_USER_AGENT} ^larbin [OR]
RewriteCond %{HTTP_USER_AGENT} ^LeechFTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mass\ Downloader [OR]
RewriteCond %{HTTP_USER_AGENT} ^MIDown\ tool [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mister\ PiX [OR]
RewriteCond %{HTTP_USER_AGENT} ^Navroad [OR]
RewriteCond %{HTTP_USER_AGENT} ^NearSite [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetAnts [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Net\ Vampire [OR]
RewriteCond %{HTTP_USER_AGENT} ^NetZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Octopus [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Explorer [OR]
RewriteCond %{HTTP_USER_AGENT} ^Offline\ Navigator [OR]
RewriteCond %{HTTP_USER_AGENT} ^PageGrabber [OR]
RewriteCond %{HTTP_USER_AGENT} ^Papa\ Foto [OR]
RewriteCond %{HTTP_USER_AGENT} ^pavuk [OR]
RewriteCond %{HTTP_USER_AGENT} ^pcBrowser [OR]
RewriteCond %{HTTP_USER_AGENT} ^RealDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^ReGet [OR]
RewriteCond %{HTTP_USER_AGENT} ^SiteSnagger [OR]
RewriteCond %{HTTP_USER_AGENT} ^SmartDownload [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperBot [OR]
RewriteCond %{HTTP_USER_AGENT} ^SuperHTTP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Surfbot [OR]
RewriteCond %{HTTP_USER_AGENT} ^tAkeOut [OR]
RewriteCond %{HTTP_USER_AGENT} ^Teleport\ Pro [OR]
RewriteCond %{HTTP_USER_AGENT} ^VoidEYE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Image\ Collector [OR]
RewriteCond %{HTTP_USER_AGENT} ^Web\ Sucker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebAuto [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebCopier [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebFetch [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebGo\ IS [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebLeacher [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebReaper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebSauger [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ eXtractor [OR]
RewriteCond %{HTTP_USER_AGENT} ^Website\ Quester [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebStripper [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebWhacker [OR]
RewriteCond %{HTTP_USER_AGENT} ^WebZIP [OR]
RewriteCond %{HTTP_USER_AGENT} ^Wget [OR]
RewriteCond %{HTTP_USER_AGENT} ^Widow [OR]
RewriteCond %{HTTP_USER_AGENT} ^WWWOFFLE [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.* - [F,L]

# We are setting here default charset & language headers setting for our website
AddDefaultCharset UTF-8
DefaultLanguage en-US

Allow from all

# Disable directory browsing
Options All -Indexes

2 个答案:

答案 0 :(得分:2)

我研究了一段时间,我学到了一些东西。

在localhost上一切正常,但是在联机时规则非常严格。 (我们都知道)

但是您发布的代码都是正确的,但是仍然无法正常工作。我尝试挖掘为什么它不起作用,因为我在日志中看不到任何错误,也没有发生500(内部错误)。

我还对代码进行了调整,发现以下内容只是我必须更改

这是我现有的代码

#code to make pretty URLS | we're using this code to achieve /category/slug
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]

在上面的评论中,我找到了与本地主机一起使用的有用答案。

以下是建议的答案

<IfModule mod_rewrite.c>
  RewriteEngine On

  #code to make pretty URLS | we're using this code to achieve /category/slug
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
  RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]
</IfModule>

即使我不使用<IfModule mod_rewrite.c>

,此代码也可以正常工作

问题仅在本节中,逻辑有点错误(代码很好,但对于正在开发的localhost)

RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]

当我问这个问题时,可以在我的.htaccess文件中使用此代码

# Over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]

以上代码将请求从基本位置重定向到/app文件夹,这是内部重定向,但在外部没有人知道确切的URL。

我的文件夹结构是

Base Folder - App - post.php .htaccess

如果我使用此代码

RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]

它将在基本文件夹中搜索文件,即与该文件位于App Folder附近的文件,而我的文件位于app文件夹中

因此,我使用了以下代码,只需在app/之前添加post.php

RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ app/post.php?folder=$1&category=$2&slug=$3 [L,QSA]
RewriteRule ^(.+)/([\w-]+)$ app/post.php?folder=$1&category=$2   [L]

它既可以在localhost上运行,也可以在生产环境(在线)上在线工作

我了解的是localhost随便使用代码。但是,就在线而言,现在不是这样,请求将在app目录内而不是app目录旁搜索文件。

答案 1 :(得分:1)

有时htaccess命令在模块内部运行。

尝试这个。

<IfModule mod_rewrite.c>
    RewriteEngine On

    #code to make pretty URLS | we're using this code to achieve /category/slug
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/([\w-]+)/([\w-]+)$ post.php?folder=$1&category=$2&slug=$3 [L,QSA]
    RewriteRule ^(.+)/([\w-]+)$ post.php?folder=$1&category=$2   [L]
</IfModule>

尝试mod_rewrite.cmod_rewrite.so

这也是测试您的正则表达式是否有效的website

更新:

请尝试以下规则:

RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ post.php?folder=$1&category=$2&slug=$3 [L]
RewriteRule ^([^/]*)/([^/]*)$ post.php?category=$1&slug=$2 [L]

还尝试在有<IfModule ...>和没有 import { area } from "./area"; import { actividad_persona } from "./actividad_persona"; import { DocumentReference } from "angularfire2/firestore"; export interface persona { id?: string nombre: string apellidos: string area: area areaRef?: DocumentReference; UserName: string password: string actividad_persona: actividad_persona[] } 的情况下运行它