htaccess rule fails sometimes but not always - .htaccess

i have the following rule to my htaccess
RewriteRule ^(el|en)/(.*).html/?$ article.php?lang=$1&url=$2 [L,NC,QSA]
When a user goes to
www.mydomain.com/en/this-is-an-article.html
the rule forward to article.php and show the page.
The problem is that sometimes the rules fails and instead of go to
www.mydomain.com/en/this-is-an-article.html
its go to
www.mydomain.com/article.php
and the page is not render because the passing parameters are not send and can not handle it
If you make another click to see the page the page render as expected.
Its the first time i see something like that.
full htaccess code
RewriteEngine on
Options -Indexes
AddDefaultCharset UTF-8
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
# WEEK
<FilesMatch "\.(pdf|swf|js|css|jpg|png|gif|JPG)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl|ttf|woff)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
#fcp.php
RewriteRule ^(el|en)/fcp.php?$ fcp.php?lang=$1 [L,NC,QSA]
#tuner.php
RewriteRule ^(el|en)/index-b/(.*).html/?$ index-b.php?lang=$1&url=$2 [L,NC,QSA]
RewriteRule ^(el|en)/catalog-b/(.*).html/?$ catalog-b.php?lang=$1&url=$2 [L,NC,QSA]
#product.php
RewriteRule ^(el|en)/product/(.*).html/?$ product.php?lang=$1&url=$2 [L,NC,QSA]
#article.php
RewriteRule ^(el|en)/(.*).html/?$ article.php?lang=$1&url=$2 [L,NC,QSA]
#controller.php
RewriteRule ^(el|en)/(.*)/p/([^.]+)?$ controller.php?lang=$1&url=$2&p=$3 [L,NC,QSA]
RewriteRule ^(el|en)/(.*)/?$ controller.php?lang=$1&url=$2 [L,NC,QSA]
#index.php
RewriteRule ^(el|en)?$ index.php?lang=$1 [L,NC,QSA]
Can anyone help

Try placing this code at top of your .htaccess:
# disable MultiViews as it conflicts with mod_rewrite
Options +FollowSymLinks -MultiViews
RewriteEngine on
# skip further rewrites if rewrite has already happened OR else
# if request is for a valid file/directory
RewriteCond %{ENV:REDIRECT_STATUS} 200 [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

Related

Replace blog with static page redirect rest to /blog with htaaccess

I have a website that currently has a WordPress blog. I want to move the blog into /blog and make the static page the new "root".
That's easy in terms of FTP, just moving folders along.
However, I'd love to get anything that would normally be 404 to get redirected to the /blog part so content doesn't get lost on the migration.
Is this doable with .hatccess?
Current .htaccess file:
#DirectoryIndex index.php index.html
#Options +FollowSymLinks
# Indexes
Options All -Indexes
# REDIRECT https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# BEGIN WordPress
# Dynamically generated by WP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
# END FileETag MTime Size<!--formatted-->
# Protecting htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
# Protecting wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>%
Assuming the "current .htaccess file" you have posted is the .htaccess file you intend to move to /blog/.htaccess. In which case you will need to change it as follows:
#DirectoryIndex index.php index.html
#Options +FollowSymLinks
# Indexes
Options All -Indexes
# REDIRECT https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
# BEGIN WordPress
# Dynamically generated by WP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
# BEGIN FileETag MTime Size
<ifmodule mod_expires.c>
<filesmatch "\.(jpg|gif|png|css|js)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
</filesmatch>
</ifmodule>
# END FileETag MTime Size<!--formatted-->
# Protecting htaccess
<files .htaccess>
order allow,deny
deny from all
</files>
# Protecting wpconfig.php
<files wp-config.php>
order allow,deny
deny from all
</files>
Notable changes:
Changed HTTP to HTTPS redirect to use the REQUEST_URI server variable instead of a backreference. With it being in a subdirectory (blog), the subdirectory would otherwise be omited from the redirect to HTTPS.
Commented out (removed) the RewriteBase directive. This is not required here, but if you did need to set this, it should be set to RewriteBase /blog.
Removed the slash prefix on the RewriteRule substitution string. ie. Changed this RewriteRule . /index.php [L] to this RewriteRule . index.php [L]
Without the RewriteBase defined, this is now relative to the current directory, ie. /blog. Without having to explicitly state the /blog directory.
You had an erroneous % at the end of the file?
I'd love to get anything that would normally be 404 to get redirected to the /blog part so content doesn't get lost on the migration.
This can perhaps be refined if we know the format of your original URLs, however, we basically need to redirect any request for anywhere outside of the /blog subdirectory - that would trigger a 404 - to be redirected to the /blog subdirectory.
You need to create a new .htaccess file in the document root with the following:
RewriteEngine On
# Redirect any 404s back to the "/blog" directory
RewriteRule %{REQUEST_FILENAME} !-f
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule !^blog/ /blog%{REQUEST_URI} [R=301,L]
The above would redirect a request for /foo to /blog/foo. But a request for /blog/bar would not be touched by this directive and routed through WordPress as normal (probably resulting in a 404 generated by WordPress).
You should first test with a 302 (temporary) redirect to avoid potential caching issues and only change to a 301 (permanent) redirect once you have confirmed it works as intended.
Try using the ErrorDocument directive
ErrorDocument 404 /blog

htaccess redirect issue due to php version upgradation

I have uploaded the PHP version from 5.3 to 5.5 for my website but it redirects to coming-soon page which I set for 401, 403, 500.
It works on old PHP version(5.3)
http://www.techmodi.com/demo/jaspee/services/getCMS?id=1
but It does not work for upgraded version i.e. 5.5
http://apis.jaspee.com/services/getCMS?id=1
I have also tried with AddHandler application/x-httpd-php55 .php but id downloads the file instead of executing.
can you please suggest the solution for the same?
root folder .htaccess file
Options -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine on
# Remove below Hash comment when your porject is going live (This is for PHP 5 support)
#AddHandler application/x-httpd-php5 .php
Options -Indexes
#Base navigational rules
DirectoryIndex index.php
RewriteRule ^index.php|index.html|default.php|default.html$ index.php [QSA,L]
#RewriteRule ^index.php$ controller/home/home.php [QSA,L]
#Admin interface URL
RewriteRule ^admin/{0,1}$ admin/index.php [QSA,L]
#User interface URLs
RewriteRule ^aboutus[/]{0,1}$ controller/cms/cms.php?id1=1
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/{0,1}$ panel.php?id1=$2
# Folder name($1) / File name($1) (Use this when your folder name and file name is same)
RewriteRule ^([a-zA-Z0-9_=-]+)/{0,1}$ controller/$1/$1.php
#Folder name($1) / File name($2)
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/{0,1}$ controller/$1/$2.php
#Folder name($1) / First Parameter($2) / File name($3)
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/{0,1}$ controller/$1/$3.php?id1=$2
#Folder name($1) / First Parameter($2) / Second Parameter($3) / File name($4)
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/{0,1}$ controller/$1/$4.php?id1=$2&id2=$3
#Folder name($1) / First Parameter($2) / Second Parameter($3) / Third Parameter($4) / File name($5)
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/{0,1}$ controller/$1/$5.php?id1=$2&id2=$3&id3=$4
#Folder name($1) / First Parameter($2) / Second Parameter($3) / Third Parameter($4) / Fourth Parameter($5) / File name($6)
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+)/{0,1}$ controller/$1/$6.php?id1=$2&id2=$3&id3=$4&id4=$5
#Folder name($1) / File name($2)
RewriteRule ^([a-zA-Z0-9_=-]+)/([a-zA-Z0-9_=-]+).html controller/$1/$2.php
# Module htaccess end
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# Page not found
ErrorDocument 400 http://apis.jaspee.com/comingsoon.php
ErrorDocument 401 http://apis.jaspee.com/comingsoon.php
ErrorDocument 403 http://apis.jaspee.com/comingsoon.php
ErrorDocument 404 http://apis.jaspee.com/404.php
ErrorDocument 500 http://apis.jaspee.com/comingsoon.php
</IfModule>
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_include mime ^images/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
# Check String ([A-Za-z]+)
# Check Number ([0-9]+)
# MIX ([a-zA-Z0-9_-]+)
web service .htaccess folder
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteBase /jspee
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ srvcRest.php?rqst=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ srvcRest.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ srvcRest.php [QSA,NC,L]
</IfModule>
<Limit GET POST PUT DELETE>
Allow from all
</Limit>

htaccess 301 redirect with GET doesn't work

What I want to achieve is the following:
URL /element.php?dogtag=tdcf_tdcbusiness_ak should redirect to /ajankohtaista
I have the following .htaccess:
RewriteCond %{QUERY_STRING} dogtag=tdcf_tdcbusiness_ak
RewriteRule ^element\.php$ /ajankohtaista/? [L,R=301]
But it doesn't work. Any clue what I am missing ?
EDIT: here is my full .htaccess:
# Protect files and directories from prying eyes.
<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$">
Order allow,deny
</FilesMatch>
# Don't show directory listings for URLs which map to a directory.
Options -Indexes
# Follow symbolic links in this directory.
Options +FollowSymLinks
# Make Drupal handle any 404 errors.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php index.html index.htm
# Override PHP settings that cannot be changed at runtime. See
# sites/default/default.settings.php and drupal_environment_initialize() in
# includes/bootstrap.inc for settings that can be changed at runtime.
# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
php_flag magic_quotes_gpc off
php_flag magic_quotes_sybase off
php_flag register_globals off
php_flag session.auto_start off
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_flag mbstring.encoding_translation off
</IfModule>
# Requires mod_expires to be enabled.
<IfModule mod_expires.c>
# Enable expirations.
ExpiresActive On
# Cache all files for 2 weeks after access (A).
ExpiresDefault A1209600
<FilesMatch \.php$>
# Do not allow PHP scripts to be cached unless they explicitly send cache
# headers themselves. Otherwise all scripts would have to overwrite the
# headers set by mod_expires if they want another caching behavior. This may
# fail if an error occurs early in the bootstrap process, and it may cause
# problems if a non-Drupal PHP file is installed in a subdirectory.
ExpiresActive Off
</FilesMatch>
</IfModule>
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule "(^|/)\." - [F]
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
RewriteCond %{QUERY_STRING} dogtag=tdcf_tdcbusiness_ak
RewriteRule ^element\.php$ /ajankohtaista/? [L,R=301]
# Rules to correctly serve gzip compressed CSS and JS files.
# Requires both mod_rewrite and mod_headers to be enabled.
<IfModule mod_headers.c>
# Serve gzip compressed CSS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.css $1\.css\.gz [QSA]
# Serve gzip compressed JS files if they exist and the client accepts gzip.
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}\.gz -s
RewriteRule ^(.*)\.js $1\.js\.gz [QSA]
# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1]
RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1]
<FilesMatch "(\.js\.gz|\.css\.gz)$">
# Serve correct encoding type.
Header set Content-Encoding gzip
# Force proxies to cache gzipped & non-gzipped css/js files separately.
Header append Vary Accept-Encoding
</FilesMatch>
</IfModule>
</IfModule>
I think you don't have element.php file anymore (it does not exist).
Because of that, it is rewritten to index.php regarding on your previous rule.
An easy solution would be to swap your two rules.
This part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
RewriteCond %{QUERY_STRING} dogtag=tdcf_tdcbusiness_ak
RewriteRule ^element\.php$ /ajankohtaista/? [L,R=301]
should be like this
RewriteCond %{QUERY_STRING} dogtag=tdcf_tdcbusiness_ak
RewriteRule ^element\.php$ /ajankohtaista [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]

new htaccess .html rule causing redirection

Please can someone help, we are having problems with our htaccess.
We have created a rule sending everything ending .html (our product pages) to our details.cfm page. However after creating this rule our admin area subdirectory is redirecting to the index page.
Have we set this up properly? I think we have an error here
# Rewrite rules for non www entrances
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.ourdomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.ourdomain.co.uk/$1 [L,R=301]
# Rewrite rules - new rule
RewriteRule (.*).html$ /Details.cfm?ProductUrl=$1 [NC]
# Rewrite rules - current rules
RewriteRule (.*)-sku-(.*)$ /Details.cfm?Name=$1&ProductCode=$2 [NC]
RewriteRule (.*)-shop-(.*)$ /Results.cfm?bname=$1&Brand=$2 [NC]
RewriteRule (.*)-cat1-(.*)$ /SubCat.cfm?catname=$1&category=$2 [NC]
RewriteRule (.*)-cat2-(.*)$ /SubSubCat.cfm?secondcatname=$1&secondary=$2 [NC]
RewriteRule (.*)-cat3-(.*)$ /Results.cfm?thirdcatname=$1&third=$2 [NC]
RewriteRule (.*)-shop$ /ResultsShop.cfm?bname=$1 [NC]
# Rewrite rule - If no extension specified use cfm
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.cfm -f
RewriteRule ^(.*)$ $1.cfm
# Rwerite rule - 404 redirect rule
ErrorDocument 404 /cferror.cfm
# Gzip rule to speed up website with image caching
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
We also have an htaccess file in the directory which checks for a htpasswd
AuthType Basic
AuthName "Admin"
AuthUserFile "/home/ourdomain/.htpasswds/public_html/v3/passwd"
require valid-user
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?v3/(.*) https://%{SERVER_NAME}/v3/$1 [R,L]
Thankyou!
Add this rule just below RewriteBase / line:
# skip v3 from rewrite
RewriteRule ^v3(/.*)?$ - [NC,L]

Can I add these rules "as-is" into my httpd.conf?

Here are all my rules with comments:
<Directory /home/*/public_html>
# prevent directory browsing
Options All -Indexes
# prevent access to htaccess files anywhere on the site
<Files .htaccess>
order allow,deny
deny from all
</Files>
# prevent DIRECT access to the /inc/ directory and its PHP files
<Files ~ "\.inc$">
order allow,deny
deny from all
</Files>
# custom error documents
ErrorDocument 400 /error/400.html
ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 500 /error/500.html
# security requirement of the rewrite engine
Options +FollowSymLinks
# turn on rewrite engine
RewriteEngine On
# remove PHP extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</Directory>
and
<Directory /home/*/public_html/images>
# caching
<IfModule mod_expires.c>
ExpiresActive On
<FilesMatch "\.(jpg|jpeg)$">
ExpiresDefault "modification plus 1 year"
Header set Cache-Control: "public, max-age=31536000"
Header unset Last-Modified
</FilesMatch>
</IfModule>
# turn on rewrite engine
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# if file cannot be found
RewriteRule ^.*$ /empty.jpg [NC,L]
</Directory>
Can I add these rules as they are to the bottom of my httpd.cnf? Or are there any mistakes? Or anything else I need to pay attention to? I don't want to screw anything up, even tho I'm just running my code on my localhost.
Any help would be appreciated. Thanks.
you can put them in httpd.cnf file! and they will work! The only thing you should pay attention is that to put them in right place(e.g in right site)

Resources