URL rewrite for image path - Multistore - .htaccess - .htaccess

I have two store (Opencart3 - Multistore), and the second store the images is not taken from the first shop folder.
I would like to solve the following:
(for example)
http://www.pce-hungary.hu/image/
replace this image path (RewriteCond %{REQUEST_FILENAME} - I think...)
https://shop.eurochrom.hu/image/
How do I fix this under .htaccess?

These two lines had to be added and so good:
RewriteCond %{REQUEST_URI} ^/image/(.)$
RewriteRule ^(.) https://shop.eurochrom.hu/image/%1 [R=302,NC]

Related

.htaccess subdomain Rewrite rule is not working

I am making a website builder an I would like to make urls prettier.
The url would be for example:
https://ccc-bb.example.com => https://example.com/project/show/ccc/bb
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\.(?!well-known/) - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-(.*)$ https://example.com/project/show/$1/$2 [L]
When I use above (https://ccc-bb.example.com) it sends me to the subdomain empty folder. The folder has only the .htaccess file.
What am I missing? I've never edited an .htaccess file and Google didn't help me (or I don't know what should I looking for).
Your first rule for dotfiles is okay but would be better the other way around, since the second part can only match the start, but the first can only match in subdirectories.
RewriteRule ^\.(?!well-known/)|/\. - [F]
Your other rule's problem is that you are expecting it to match the subdomain. RewriteRules do not operate on the entire string you see in your browser's address bar, only the path part, and in .htaccess they see even less as the leading directory is stripped off, too. To access the info you want, use a RewriteCond:
RewriteCond %{HTTP_HOST} ^([^-]++)-([^-.]++)\.example\.com$
RewriteRule ^(?!project/show/).* project/show/%1/%2/$0 [L,DPI]
(You don't need to include \.example\.com$ if your main domain contains no hyphens.)

mode_rewrite (.htaccess) wrong outcome

I'm not sure what's wrong. I use massively rewrite on to domain with various rules with no problem, now here on subdomain the rewritten result point wrong data.
The space is accessible from multiple domains, and I want to switch by hostname to specific subfolder for CSS contents:
RewriteEngine on
# - - - shared space / multiple css.*.tld subdomains - - -
# domain 1
RewriteCond %{HTTP_HOST} ^css\.firstdomain\.com$
RewriteRule ^css/(.*)$ fir/$1
# domain 2
RewriteCond %{HTTP_HOST} ^css\.seconddomain2\.com$
RewriteRule ^css/(.*)$ sec/$1
# *.min.css -> scss.php?file=*
RewriteRule ^(.+)\.min\.css$ scss.php?scss=$1 [QSA]
#
First there are rules to rewrite universal /css/ path to path specific for files for that host, then last line should change *.min.css to scss.php?scss=$1 - so it send the file /without the extension .min.css/ as parameter to php file which then searches for that file with .scss extension to check last modified and either return cached or recompile the source scss file to cached css file
Now I'd expect when I enter: http://css.firstdomain.com/css/first/first.min.css
should rewrite to: http://css.firstdomain.com/scss.php?file=fir/first/first.min.css
but it rewrites to: http://css.firstdomain.com/scss.php?file=fir/first/first.min.css/first/first
So it rewrites it like almost twice for some reason. What might be the reason for this?
There will be problem of some kind with the firs part as entering straight http://css.firstdomain.com/fir/first/first.min.css
rewrites to correct (scss.php?file=fir/first/first).
Oooh found it! I missed the [L] (last) flag after rules for each domain, now it works like it should
For completeness, here is the code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^css\.firstdomain\.com$
RewriteRule ^css/(.*)$ fir/$1 [QSA,L]
RewriteRule ^(.+)\.min\.css$ scss.php?scss=$1 [QSA,L]

Generic .htaccess folder redirect with variable root paths

I want to do a simple redirect so that a request for app/scans/large/as89q6dfa.jpg results in app/scans/medium/as89q6dfa.jpg, etc. The trouble is that this app will be used on a few domains with different base paths. The code I've tried keeps rewriting to the absolute base path of the site and the app is actually a variable number of folders deep on the site. Is there a generalized way to do a redirect like this, without hard-coding the base path?
Here's my file and folder scructure:
app/.htaccess
app/scans/large
app/scans/medium
So the .htaccess rules should work for:
includes/app/scans/large
inc/app/scans/large
script/engine/app/scans/large
Here's my first attempt:
RedirectMatch 301 scans/large/(.*) scans/medium//$1
Here's my second attempt:
RewriteEngine On
RewriteBase /
RewriteRule ^scans/large/(.*) scans/medium/$1 [R=301,L]
When you use the "^" caracter it means that the regex will search for the string "scans/large" in the BEGINNING of the URL.
So as the base is "/", it won't work on "includes/app/scans/large/omg" because "scans" it's not in the beginning of the string.
Try this solution:
RewriteEngine On
RewriteBase /
RewriteRule (.*)scans/large/(.*) $1scans/medium/$2 [R=301,L]
It worked perfectly with the URL:
http://www.example.com/includes/app/scans/large/omg
That redirects to:
http://www.example.com/includes/app/scans/medium/omg
You can do more tests on it here: link
You can actually get RewriteBase be determined dynamically.
RewriteEngine On
# determine BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteRule ^scans/large/(.*)$ %{ENV:BASE}scans/medium/$1 [R=301,L,NC]

Using htaccess to rewrite URL using an ID to a JPG filename

I currently have URLs like this:
/video/245008/245008_00000001.jpg
/video/245008/245008_00000002.jpg
etc.
I need htaccess to rewrite the above urls to be:
/video/245008/245008_1.jpg
/video/245008/245008_2.jpg
But only if the original 00000001.jpg, etc do not exist. Any suggestions?
Try adding these rules to the htaccess file in your document root (the one that has the video directory):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/video/([0-9]+)/\1_0+([1-9][0-9]*)\.jpg$
RewriteCond %{DOCUMENT_ROOT}/video/%1/%1_%2.jpg -f
RewriteRule ^ /video/%1/%1_%2.jpg [L]
The first condition checks that the current request points to a file that doesn't exist. The second condition checks that the request is for something in the video directory, 2 gorups of identical numbers, and underscore with at least one zero following it, then some more numbers. The third condition checks that the new URI points to a file that does exist.

How to setup optional parameters in mod_rewrite

I am in a new project and I'm designing the URL structure,
the thing is I want URLs look like this:
/category-23/keyword/5/
Where the normal page is:
/search.php?q=keyword&cat=23&page=5
So my question is, cat and page fields, must be optional, I mean if I go to /keyword it should be
/search.php?q=keyword (page 1)
and if I go to
/category/keyword should be:
/search.php?q=keyword&cat=category&p=1
and also if I go to
/keyword/5/ it must be: /search.php?q=keyword&p=5
Now I have my .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.*)$ search.php?q=$2&cat=$1&page=$3 [L]
I cannot make it working and the CSS / image files don't load.
I'd thank a lot who could give me a solution.
You can do this with four rules, one for each case:
RewriteRule ^([^/]+)$ search.php?q=$1
RewriteRule ^([^/]+)/([0-9]+)$ search.php?q=$2&p=$1
RewriteRule ^([^/]+)/([^/]+)$ search.php?q=$2&cat=$1&p=1
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)$ search.php?q=$2&cat=$1&p=$3
And with this rule in front of the other rules, any request that can be mapped onto existing files will be passed through:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^ - [L]
Now your last issue, that externally linked resources cannot be found, is due to that you’re probably using relative URL paths like css/style.css or ./css/style.css. These relative references are resolved from the base URL path that is the URL path of the URL of the document the references are used in. So in case /category/keyword is requested, a relative reference like css/style.css is resolved to /category/keyword/css/style.css and not /css/style.css. Using the absolute URL path /css/style.css makes it independent from the actual base URL path
While i know this was answered succinctly by #Gumbo a few months back, I ran into a similar issue recently... and didn't want to include full/absolute paths in my app, to keep it dynamic and not having a bunch APP_PATH (php) vars all over the place... so I just added a base[href] html tag with the
like so...
<base href="http://<?php echo $_SERVER[HTTP_HOST];?><?php echo APP_PATH;?>"/>
Hoping that helps others... and this is not trying to discount #Gumbo's reply in the least... they're right-e-o on :).
Shouldn't it be
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^ - [L]
in #Gumbo's answer above? Works with me like that. If it is a file (like CSS), let it pass through.

Resources