url rewrite for subfolder - apache

I'm trying to do rewrite only for a subdirectory, but I have a problem with RewriteCond.
All my files are in /abc.
Here is my htaccess (/abc/.htaccess) file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/static/ [NC,OR]
RewriteCond %{REQUEST_URI} ^/files/ [NC]
RewriteRule . - [L]
RewriteRule (.*) index.php?_url=$1 [QSA,L]
So I want to rewrite every url to index.php except the existing files in /static and /files. But this code is rewriting /static/a.jpg too, since the request was /abc/static/a.jpg. I tried setting RewriteBase to /abc/, but nothing changed.
Any idea how can I solve this?

Have your .htaccess like this in /abc/.htaccess:
RewriteEngine on
RewriteBase /abc/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^/(static|files)(/|$) [NC]
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?_url=$1 [QSA,L]

Related

Long URL in php using htaccess

I have a very long URL: //domain/administration/registrar/term_detail.php?a_id=47
And I want to make it looks like: //domain/administration/registrar/detail/47
I have created an htaccess file in //domain/administration/registrar/. Below is my code in htaccess and it is not working. Any suggestion of what I did wrong is appreciated.
Both of my htaccess and term_detail.php files are inside of registrar folder.
RewriteEngine On
RewriteBase /administration/registrar/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+registrar/detail\.php\?a_id=([^\s&]+) [NC]
RewriteRule ^ administration/registrar/%1? [R=301,L]
RewriteRule ^administration/registrar/([a-zA-Z0-9]+)/?$ detail?a_id=$1 [L,QSA,NC]
You can try these rules inside your /administration/registrar/.htaccess:
RewriteEngine On
RewriteBase /administration/registrar/
## External redirect to /administration/registrar/term_detail/47
RewriteCond %{THE_REQUEST} /([^./]+)(?:\.php)?\?a_id=([^&\s]+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]
## Internal rewrite to term_detail.php?a_id=47
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?a_id=$2 [QSA,L]
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Keep your htaccess file inside registrar folder.
RewriteEngine ON
##External redirect to //domain/administration/registrar/detail/47 URL.
RewriteBase /administration/registrar/
RewriteCond %{THE_REQUEST} \s/([^/]*)/([^/]*)/(?:[^_]*)_([^.]*)\.php\?a_id=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=301,L]
##Internal rewrite to term_detail.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ $1/$2/term_$3.php?a_id=$4 [QSA,L]

htaccess rewriting subdomain to a folder

I want to rewrite my main domain to a path and if no query is appended then to a different path. I have a wildcard C name in place to support subdomains.
www.xxx.com => works normally
abc.xxx.com => point to a folder
abc.xxx.com/abc-xyz => point to www.xxx.com and should work normally
This is what i have right now :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}example.com$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]example.com$
RewriteRule ^$ portfolio/index.php?pageDetailId=%1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{HTTP_HOST} !^([w]{3,3}[.]{1,1}){0,1}example.com$
RewriteCond %{HTTP_HOST} ^([0-9a-zA-Z-]*)[.]example.com$
RewriteRule ^(.*)$ $1?pageDetailId=%1 [QSA,L]
</IfModule>
As we can see, i need that abc as variable in abc.example.com/abc-xyz. Please reply with doubts with any, i tried many ways its not working. This same htaccess works on 1 domain smoothly but when i created a new domain its not working on that. Thanks.
NOTE: It works on httpd but not on apache2
Have it like this:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)([\w-]+)\.example\.com$ [NC]
RewriteRule ^(index\.php)?$ portfolio/index.php?pageDetailId=%1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(?!www\.)([\w-]+)\.example\.com$ [NC]
RewriteRule ^(.+)$ index.php?pageDetailId=%1&/$1 [L,QSA]
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

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]

Exclude directory from rewrite rule

Below is my .htaccess file. I am trying to exclude the rewrite rules being applied to the /admin/ folder, however after spending an hour researching and trying various codes I have reached a wall.
Options -MultiViews
RewriteEngine On
RewriteBase /
# Exclude admin directory
RewriteRule ^(admin|admin)($|/) - [L]
RewriteRule ^privacy$ pages/privacy.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/(.+)$ products.php?q=$3&rewrite=1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)/$ search.php?q=bw:&categoryFilter=$1&genreFilter=$2%{QUERY_STRING} [L,B]
RewriteRule ^(.*)/$ pages/format.php?format=$1 [NC,L}
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
The above code brings an internal 500 error and if I remove it will redirect me to the rewrite rule for the format. I was reading that The exclusion rewrite rule needs to be placed before the rule I want to overwrite. I don't understand why it's not working and what I have missed.
Thanks.
Try and write your rule this way.
# Exclude admin directory
RewriteRule ^admin/?$ - [L]
You have a syntax error in your .htaccess.
Have your .htaccess like this:
Options -MultiViews
RewriteEngine On
RewriteBase /
# Exclude admin directory
RewriteRule ^admin($|/) - [L,NC]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^privacy$ pages/privacy.php [NC,L]
RewriteRule ^(.+)/(.+)/(.+)$ products.php?q=$3&rewrite=1 [L,QSA]
RewriteRule ^(.*)/(.*)/$ search.php?q=bw:&categoryFilter=$1&genreFilter=$2 [L,B,QSA]
RewriteRule ^(.+)/$ pages/format.php?format=$1 [NC,L]

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1