skip to Main Content

I’m currently trying to make a htaccess thing to find the file in new/ if the request uri not contains api or list. I tried to do a thing like this but, it shows 403 error. Are there a easier way to filter, or?

RewriteEngine on 
RewriteCond %{REQUEST_URI} /api/ [NC]
RewriteCond %{REQUEST_URI} /list/ [NC]
RewriteRule ^/(.*)$/ new/$1 [L, R]

Full htaccess code

#remove html file extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L] 

#set max post size
php_value memory_limit 2048M
php_value max_execution_time 3000
php_value max_input_time 6000
php_value post_max_size 800M
php_value upload_max_filesize 200M

#Caching schema
<FilesMatch ".(png|svg|css|eot|ttf|woff|woff2|otf|js|css)$">
Header set Cache-Control "public, max-age=86400000"
</FilesMatch>

DirectoryIndex index.html

#Custom 404 errors
ErrorDocument 404 /404.html

#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

<Files error_log>
order allow,deny
deny from all
</Files>

#Prevent directory listings
Options All -Indexes

2

Answers


  1. EDIT: With OP’s full htaccess file please try following.

    #remove html file extension
    RewriteEngine on 
    ##For external redirect rule.
    RewriteCond %{THE_REQUEST} !s/(?:api|list).*s [NC]
    RewriteRule ^(.*)/?$ new/$1 [L,R=301]
    
    ##For internal redirect rule.
    RewriteRule ^new/(.*)/?$ $1 [L,NC]
    
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*)/?$ $1.html [NC,L]
    
    #remove php file extension
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)/?$ $1.php [QSA,NC,L] 
    
    #set max post size
    php_value memory_limit 2048M
    php_value max_execution_time 3000
    php_value max_input_time 6000
    php_value post_max_size 800M
    php_value upload_max_filesize 200M
    
    #Caching schema
    <FilesMatch ".(png|svg|css|eot|ttf|woff|woff2|otf|js|css)$">
    Header set Cache-Control "public, max-age=86400000"
    </FilesMatch>
    
    DirectoryIndex index.html
    
    #Custom 404 errors
    ErrorDocument 404 /404.html
    
    #Prevent viewing of .htaccess file
    <Files .htaccess>
    order allow,deny
    deny from all
    </Files>
    
    <Files error_log>
    order allow,deny
    deny from all
    </Files>
    
    #Prevent directory listings
    Options All -Indexes
    


    With your shown attempts/rules, could you please try following once. Please make sure to clear browser cache before testing your URLs.

    RewriteEngine on 
    ##For external redirect rule.
    RewriteCond %{REQUEST_URI} !^/(?:api|list)/? [NC]
    RewriteRule ^(.*)/?$ new/$1 [L,R=301]
    
    ##For internal redirect rule.
    RewriteRule ^new/(.*)/?$ $1 [L,NC]
    

    OR try following rules set: Please make sure try only 1 set at a time, either above OR following.

    RewriteEngine on 
    ##For external redirect rule.
    RewriteCond %{THE_REQUEST} !s/(?:api|list).*s [NC]
    RewriteRule ^(.*)/?$ new/$1 [L,R=301]
    
    ##For internal redirect rule.
    RewriteRule ^new/(.*)/?$ $1 [L,NC]
    
    Login or Signup to reply.
  2. You may try these rules in your site root .htaccess:

    RewriteEngine On 
    
    # redirect to /new if not /api, /list, /new and file exists in /new
    RewriteCond %{THE_REQUEST} !s/+(new|api|list)/ [NC]
    RewriteCond %{DOCUMENT_ROOT}/new/$0 -f
    RewriteRule .* /new/$0 [L,R=301,NE]
    
    ## remaining rules go below this line ##
    
    # remove html file extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^(.*?)/?$ $1.html [L]
    
    # remove php file extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L] 
    

    Make sure to test it after clearing your browser cache or test from a new browser.

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