skip to Main Content

I am using below code to make pretty 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]

This code seems to be working very perfect on localhost, but as soon as I deployed it online, it didn’t work, it didn’t show any internal error it shows 404 not found. As if it is looking for a folder or file.

Where is the error or am I missing any logic that should be kept in mind, on localhost and online.

I am having a URL that has been redirected to a file

www.xyz.com/technology (This URL only for getting category)
www.xyz.com/technology/some-post (This URL only for getting category and slug)

I have some URLs:

www.xyz.com/post.php?category=technology&slug=some-slug
to
www.xyz.com/technology/some-slug (Now this rule, if it has only one parameter I’ll run query only for a category if it has 2 params then ill run for category and slug)

This is my whole .htaccess code

<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:[email protected] [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

Answers


  1. Chosen as BEST ANSWER

    I was researching a bit for a while, I learnt a few things.

    Things work pretty great on localhost but the rules work very strictly when it comes to online. (We're all aware of it)

    However the codes you posted were all correct but still, it was not working. I tried digging up why isn't it working, as I can't see any error in the log, also there are no 500(Internal Error) going on.

    Also, I tweaked the code and I found that the following thing just I had to change

    This was my existing code

    #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]
    

    For which in above comments I found useful answers which are working with localhost.

    Following was the answer proposed

    <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>
    

    This code works fine even I don't use <IfModule mod_rewrite.c>

    The problem was only in this section, the logic was a bit wrong (The code is fine but for localhost where you're working for development)

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

    I used this code in my .htaccess file as you can see while i asked the question

    # 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]
    

    The above code redirected requests from the base location to /app folder this was an internal redirect but externally no one would know what's the URL exactly.

    My folder structure is

    Base Folder - App - post.php .htaccess

    If I use this code

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

    it will search for the file in Base Folder i.e the file which resides near App Folder with it, whereas my file was inside app folder

    So, I used the following code, just add app/ before 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]
    

    and it worked on localhost as well as online on production(Online)

    What I understood is localhost takes code casually. But, in terms of online, it doesn't so now the request searches for a file inside the app directory and not beside the app directory.


  2. Sometimes the htaccess commands work inside modules.

    Try this one.

    <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>
    

    Try either mod_rewrite.c or mod_rewrite.so

    Also this is a brilliant website to test if your regex works.

    UPDATE:

    Please try these rules:

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

    Also try to run it with and without <IfModule ...>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search