htaccess redirect for virtuemart - regex

I've setup a virtuemart ecommerce.
All ok with htaccess and redirect, store is in
http://www.domainname.com/it/store/
But the "only" problem is it that both url are valid:
http://www.domainname.com/it/store/article.html
and (note absence of "store")
http://www.domainname.com/it/article.html
creating problem with google and overall that in this mode article.html inherit settings of home page (banners, etc).
So, i would to redirect via htaccess any request sent to an URL without store to URL with /store.
.htaccess is default of Joomla:
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

Put this rule just below RewriteBase line:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/[^/]+/store/ [NC]
RewriteRule ^([^/]+)/(.+)$ /$1/store/$2 [R=301,L]

Related

I'm stuck at trying to redirect all urls in specific path to lower case

Im able to get all my urls to redirect to lower case using the following in my conf file
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
works like a charm...
The issue is though, I really need this to apply only to URLs www.example.com/this-doesnt-change/Random_Random_111
or www.example.com/this-doesnt-change/Random/Random_111
or even www.example.com/this-doesnt-change/Random
Basically anything after www.example.com/this-doesnt-change/
I've tried replacing
RewriteCond %{REQUEST_URI} [A-Z]
with
RewriteCond %{REQUEST_URI} this-doesnt-change/.+
but all I get is "too many redirects" error
Or should I be restricting the rule to this path using a function of the conf file?
Any insight is appreciated
EDIT:
the .htaccess file
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line 'Options +FollowSymLinks' may cause problems with some server configurations.
# It is required for the use of mod_rewrite, but it may have already been set by your
# server administrator in a way that disallows changing it in this .htaccess file.
# If using it causes your site to produce an error, comment it out (add # to the
# beginning of the line), reload your site in your browser and test your sef urls. If
# they work, then it has been set by your server administrator and you do not need to
# set it here.
##
RewriteRule \.(jpe?g|png|gif|ico|bmp|pdf|docx?|txt|css|js)$ - [L,NC]
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]
RewriteRule ^(server-info|server-status) - [L]
RewriteCond %{REQUEST_URI} !pagespeed
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
</IfModule>
## Mod_rewrite in use.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
RewriteRule ^(this-doesnt-change/[a-zA-Z-]+)_([^/]+)$ /$1/$2 [R=301,L]
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment the following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
php_value upload_max_filesize 5M
php_value post_max_size 10M
the
Thank you Ryszard Czech as your answer ( comment) worked ( I initially put the code in the wrong place)
Replaced the code to this and it worked like a charm.
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(/this-doesnt-change/.*) ${lc:$1} [R=301,L]
Use
RewriteRule ^(/this-doesnt-change/.*) ${lc:$1} [R=301,L]
Regex demo here
Regex explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
/this-doesnt- '/this-doesnt-change/'
change/
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
) end of \1

.htaccess file blocks css, js but how?

I have a .htaccess file inside an existing project folder. The code for the .htaccess file is below.
# Use PHP54 Single php.ini as default
#Options +FollowSymLinks
#Options +Indexes
RewriteEngine On
RewriteBase /
#RewriteRule ^users/([A-Za-z0-9_]+)$ users/index.php [NC,L]
#RewriteRule ^([A-Za-z0-9_]+)$ users/$1 [NC,L]
#RewriteRule ^ - [L]
#RewriteCond %{DOCUMENT_ROOT}/users/$1 -f
#RewriteRule .* users/$1 [L]
#Options -Indexes
#RewriteEngine On
#RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]
#<FilesMatch "\.(?i:vcf)$">
# ForceType application/octet-stream
# Header set Content-Disposition attachment
#</FilesMatch>
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /users
RewriteCond %{DOCUMENT_ROOT}/users/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/users/$1 -d
RewriteRule (.*) users/$1 [L]
#
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) users/index.php?q=$1 [L]
So what happens it, when I try to open the project in url at localhost
http://localhost/projectfolder
it opens the index file without any javascript and css. Also the links to other pages do not open and the error message shows as -"Object not found". So I can only view index.php with out css and js files.
But when I remove / rename the .htaccess file, all works fine. So there must be something in this file which blocks all css, js files. So I am not asking for all the rules of writing a .htaccess file, I just need to know which line of code is blocking the files and how can I edit them, so it will not block any file wile working in localhost.
Note:- This question I am asking only for my knowledge. As I have written, if I remove the file then its working fine. But still I want to know which line of code is blocking the access to files. Thanks.
I suspect this rule is not doing what you think it should be doing:
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
Replace this with:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
Also you might be using relative links for css/js/images which will also cause 404 after rewriting to /users/. You have 2 options to fix that:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's HTML header:
<base href="/" />

How do I set 301 redirects to strip off .html file extensions?

I am working on migrating a MovableType blog to Wordpress.
All of the old post urls end in .html.
Example:
http://ec2-174-129-85-183.compute-1.amazonaws.com/archives/2013/12/all_quiet_on_th.html
However, when I updated the post names in wordpress to include the .html extensions, I kept getting 404 errors.
So what I want to do now is redirect all of the requests for pages that end in .html to the same pages but without the .html.
For example:
.../archives/2013/12/all_quiet_on_th.html
Would 301 redirect to:
.../archives/2013/12/all_quiet_on_th
Here is my current .htaccess file:
# BEGIN WordPress
# Ensure the rewriet module is loaded
<IfModule mod_rewrite.c>
# enable the rewrite engine
RewriteEngine On
# Set the root directory
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Can you please advise what changes I can make to this file for this to work?
I tried 5 or 6 different versions on the internet already, but it is not working and I am not sure why not.
You can try these rules:
# BEGIN WordPress
# Ensure the rewriet module is loaded
<IfModule mod_rewrite.c>
# enable the rewrite engine
RewriteEngine On
# Set the root directory
RewriteBase /
RewriteRule ^(.+?)\.html?$ /$1 [L,NC,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

.htaccess redirection of index.php

The below illustrates the .htaccess code for a generating SEF url when joomla SEF is on in the backend.
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
I have a simple problem which I'm not able to get my head around into.
I put this condition so that if the url is domain.com/index.php then it should go to domain.com and this should happen only if the pattern matches exactly as index.php.
However this happens for all urls.
RewriteCond %{THE_REQUEST} [^.]*|/(index.php)$
RewriteRule ^index.php$ / [R=301,L]
it should go to domain.com and this should happen only if the pattern matches exactly as index.php. However this happens for all urls.
It is due to the wrong regex you're using. Use this rule to remove index.php from URIs:
RewriteCond %{REQUEST_URI} !/administrator [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

append path directory to url using htaccess

how could i get Apache to redirect
http://localhost/index.php
http://localhost/create/index.php
http://localhost/create/contact.php
http://localhost/engage/page1/services.php
to
http://localhost/Project1/index.php
http://localhost/Project1/create/index.php
http://localhost/Project1/create/contact.php
http://localhost/Project1/engage/page1/services.php
respectively?
Essentially I need to append "Project1" (or whatever other string I see fit) to the BEGINNING of the url path
Thanks
You can use negative lookahead like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# redirect to /Project1/ if it is not already /Project1/
RewriteRule ^((?!Project1/).*)$ Project1//$1 [L,NC]