If/else and file_exists function in htaccess mod_rewrite - apache

I don't really know how to explain this situation but i give it a try. So, I have this url:
http://localhost/static/some_picture.png, and my folders look something like this:
statics/x/picture.png
statics/y/logo.png
What I want is that if the user types the mentioned url, he will see the "picture.png" what is in the "statics/x/picture.png", but if he types http://localhost/static/some_logo.png then he will see the "logo.png" in the "statics/y/logo.png".
My last try:
RewriteEngine On
RewriteCond statics/x/$1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteCond statics/y/$1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
Currently i have this .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

You're on the right track, but your -f condition isn't right.
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^ /statics/x/%1 [L]
RewriteCond %{REQUEST_URI} ^/static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^ /statics/y/%1 [L]

You could try the following;
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/x/%1 -f
RewriteRule ^static/some_(.*)$ statics/x/$1 [L]
RewriteEngine On
RewriteRule %{REQUEST_FILENAME} ^static/some_(.*)$
RewriteCond %{DOCUMENT_ROOT}/statics/y/%1 -f
RewriteRule ^static/some_(.*)$ statics/y/$1 [L]
the trick is to use RewriteCond backreference , which syntax is like %1 , to match grouped parts on the previous RewriteCond.
Also, thanks Jon Lin for noticing that the RewriteCond -f option needs an absolute local path.
Update: Added %{DOCUMENT_ROOT} to RewriteCond -f , found out by Jon Lin

Related

How rewrite url in htaccess

Good Morning,
I have documents within the structure of my website, which are accessed with the following url format:
http://example.com/docs/files/123/mydoc.pdf
http://example.com/docs/files/475/otherdoc.pdf
I want when a url of the above type is accessed, it is renamed with the following format:
http://example.com/123/mydoc
http://example.com/475/otherdoc
I have the following rules in the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^/docs/files/([0-9]+)/([a-zA-Z0-9-]+)\.([a-zA-Z-]+) /$1/$2 [QSA,L]
</IfModule>
If I type in the browser
http://example.com/docs/files/123/mydoc.pdf
It doesn't change the url to
http://example.com/123/mydoc
What am I doing wrong?
Thanks
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+files/(\S+)\.pdf[?\s] [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/files/$1.pdf -f
RewriteRule ^(.+?)/?$ files/$1.pdf [L]
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s(/files/.*)\.pdf\s [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,NE]
RewriteRule ^(.*)/?$ files/$1.pdf [L]

How can I get in one spesific folder?

I have made clean urls in .htaccess file, but after that I dont get Access to a spesific folder. How can I get Access to /folder/?
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?side=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?side=$1 [QSA]
RewriteRule ^([a-z]+)\/([a-zA-Z0-9]+)\/?$ index.php?side=$1&id=$2 [NC]
RewriteRule ^rss/feed/([a-zA-Z0-9]+)\/?$ index.php?side=feed&id=$1 [NC]
You can use the following :
Options +FollowSymLinks
RewriteEngine On
##-if an existent dir or file is requested, serve it immideatly##
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
########
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?side=$1 [QSA,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/?$ index.php?side=$1&id=$2 [NC,L]
RewriteRule ^rss/feed/([a-zA-Z0-9]+)\/?$ index.php?side=feed&id=$1 [NC,L]

.htaccess multiple rewrite rules on multiple php files not working

I'm new on .htaccess and rewrite rules. So if my question is not relevant, please forgive me.
I have below Rewrite rule;
RewriteRule ^\yazarlar$ /writer-list.php [L]
I'm trying to achieve if someone hit url http://example.com/yazarlar or http://example.com/yazarlar/ they should see http://example.com/writer-list.php file. But it's not working.
Am i missing something?
FULL .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L]
RewriteRule ^([^/]+)/([0-9]?)?$ /article-list.php?link=$1&page=$2 [L]
RewriteRule ^([^/]+)?$ /article-list.php?link=$1 [L]
RewriteRule ^\yazarlar$ /writer-list.php [L]
No need to escape the \y and remove redundant rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^yazarlar/?$ writer-list.php [L,NC]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L,QSA,NC]
RewriteRule ^([^/]+)/([0-9]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ article-list.php?link=$1 [L,QSA]

.htaccess redirect if file exists in subfolder

I have the following challenge. Assuming the following file is requested from the server:
/pages/subfolder/mypage.php
Is there an option using mod-rewrite to do the following:
Look into a specific folder f. e. /USERMOD if the requested file (and structure) exists
If Yes, execute this file with all parameters
If Not, execute the requested file.
The background would be that the rewrite looks if there is a replacement under /USERMOD having the same folder structure. If an replacement exists use this one otherwise us the original file.
Actually (but doesn't work) I have is:
RewriteCond %{REQUEST_URI} !^/USERMOD/ [NC]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
Thanks for any hint and help!
EDIT: The full .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/USERMOD/ [NC]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
RewriteCond %{REQUEST_FILENAME} ^(.*)\.(php|css|js|gif|jpg|jpeg|png)$ [NC]
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_URI} (.*)?/admin/(.*)
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_URI} (.*)?/images/(.*)
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_URI} (.*)?/templates/(.*)
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.+) - [L]
##boosted CONTENT
RewriteRule (.*/)?info/([A-Za-z0-9_-]+)\.html.* shop_content.php?gm_boosted_content=$2&%{QUERY_STRING} [PT,L]
##boosted PRODUCTS
RewriteRule (.*/)?([A-Za-z0-9_-]+)\.html product_info.php?gm_boosted_product=$2&%{QUERY_STRING} [PT,L]
##boosted CATEGORIES
RewriteRule (.*/)?([A-Za-z0-9_-]+)/?.* index.php?gm_boosted_category=$2&%{QUERY_STRING} [L]
Have your initial rules like this:
RewriteEngine On
RewriteRule ^USERMOD/ - [NC,L]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
# rest of your rules come here

htaccess to rewrite file in subfolder (2nd level) to folder (1st level)

Currently, I'm using the below htaccess code to load http://www.domain.com/folder/file.php from http://www.domain.com/file, removing the folder name "/folder/", and the file extension ".php", from the URL.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Now, I'm trying to do almost the same, but with a file that is stored in a subfolder.
So, in other words, I want to call http://www.domain.com/folder/subfolder/file.php from http://www.domain.com/subfolder/file
In this case, hiding the extension file is not a problem as the 2 last lines from the code above works globally, the issue is with the Rewriting which doesn't work simply by adding the 'subfolder' name. I thought this was going to work, but it wasn't:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI}\.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}/folder/subfolder%{REQUEST_URI} -d
RewriteRule ^(.*)$ /folder/subfolder/$1.php [L]
I appreciate any help.
Replace both of your rules with this:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/folder/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /folder/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]