I have problem with my rewrite rule. I have new webpage in root/hrp. and if i open pages in that directory, then all is OK, but when i wanna surf those pages from root (without /hrp/) then is problem. I found some .htaccess rules and all working fine, but if i wanna execute some pdf,jpg,or php script directly, then come problem.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule ^(.*)$ /index.php [L]
if i put this bottom code on begining, i can open files directli, but pages don't work and vice versa
RewriteCond %{REQUEST_FILENAME} (/|\.php|\.html|\.htm|\.png|\.jpg|\.jpeg|\.gif|\.xml|\.rss|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule ^(.+)$ $1 [L]
Try adding this at the top instead:
RewriteRule \.(php|html?|png|jpe?g|gif|xml|rss|feed|pdf|raw)$ - [L,NC]
or you can try adding this condition to your hrp rule:
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/hrp/$1 -d
RewriteRule (.*) /hrp/$1 [L]
I have find solution
don't rewrite this files
RewriteCond %{REQUEST_URI} !.(gif|jpg|png|ico|css|js|swf|wav|mp3|less|cur|pdf|jpeg|txt|ico|zip|gz) [NC]
don't rewrite this folder - there i have php files, and dont rewrite this files
RewriteCond %{REQUEST_URI} !(folder1|folder2|file1.php|file2.php|file3.php) [NC]
all content is in "/hrp/" folder
RewriteCond %{REQUEST_URI} !hrp/
RewriteRule (.*) /hrp/$1 [L]
but url don't contains "/hrp/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/hrp/index.php
RewriteRule (.*) /index.php [L]
Related
I have some php files in the root directory and a directory containing some php files which I want to exclude from codeigniter. I have this code in htaccess file
RewriteCond %{REQUEST_URI} !^subdomain_name\. [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(directory_to_exclude)$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteRule ^(.*)$ index.php/$1 [L]
When I navigate to example.com/directory_to_exclude I get a list of php files (I'll add an index to prevent listing) but when I try to run any of them it reloads the codeigniter homepage. So how to fix this and how to exclude any php file in the root directory like example.com/test.php
Change your last rule to this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} !\s/+[^/.]+\.php[?\s/] [NC]
RewriteCond %{THE_REQUEST} !\s/+directory_to_exclude/ [NC]
RewriteCond %{THE_REQUEST} !\s/+[0-9]+\..+\.cpaneldcv [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{THE_REQUEST} !\s/+[^/.]+\.php[?\s/] [NC] will exclude all .php files in site root directory.
You shoud use THE_REQUEST instead of REQUEST_URI because it doesn't get overwritten after execution of some rewrite rules.
EDIT: If this doesn't work then change file permission to 644. (this actually solved OP's problem)
I have created my first Php solution but have trouble removing the .php extension from the URL. It works in the root but not in sub-folders?
Eg www.domain.com/index.php reached from www.domain.com/index (or www.domain.com/)
Then it gets tricky.
For example. www.domain.com/en/europe/bikes/racer-bike.php will not differ from www.domain.com/en/bikes/racer-bike without ending up on www.domain.com/en/bikes/ and on index.php
My .htaccess file looks like this:
Options -MultiViews
RewriteEngine on
RewriteBase /
# Remove .php-extension from url
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# SSL
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^/?(.*)$ https://%{SERVER_NAME}/$1 [L,R=301,QSA]
I Try it and it works:
My dirs structure
/
index.php
/sub
file.php
And my htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^\/]*)$
RewriteRule ^([^\/]*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ([A-Za-z]+)/(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteRule ^([A-Za-z]+)/(.*)$ $1/$2.php [NC,L,QSA]
Try it
You are doing it wrong ... Mostly you do not make php files for each uri (route). You should not remove php extension you should direct all uri's to index.php and use some router to define how to handle each request. or you can just use some framework to do that like laravel or symfony etc.
I have CodeIgniter setup in a sub-directory of a sub-domain. My htaccess file works for the following: http://subdomain.domain.com/subdirectory/
The working htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
However this does not work when I point a domain name http://www.mydomainname.org to public_html/subdirectory.
What I'm attempting to achieve is I'd like to use http://www.mydomainname.org/whatever/ to mask http://sub.domain.com/subdirectory/index.php/whatever
Is this possible? Is there anything I can add to my htaccess that will allow me to achieve this?
I attempted to use the htaccess answer here, but had no luck.
I think if you want both to work, you'll need to add extra conditions for the hostname, try something like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# for main domain
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainname\.com$ [NC]
RewriteRule ^$ /index.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomainname\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php?/$1 [L]
# for subdomain
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteRule ^$ /subdirectory/index.php [L]
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(?:subdirectory/|)(.*)$ /subdirectory/index.php?/$1 [L]
I have a .htaccess file that has multiple rules to make the url's look "pretty".
This is the file:
RewriteEngine On
RewriteRule ^property/([^/]*)/?([^/]*)/?$ /property.php?ID=$1&Image=$2 [L]
RewriteRule ^property$ /property.php [L]
RewriteRule ^enquire/([^/]*)/?([^/]*)/?$ /enquire.php?ID=$1&Data=$2 [L]
RewriteRule ^enquire$ /enquire.php [L]
RewriteRule ^contact/?$ /contact.php [L]
RewriteRule ^home/?$ /index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/(property|property/.*|enquire|enquire/.*|contact|contact/.*|home|home/.*)$
RewriteRule ^([^/]*)$ /custompages.php?ID=$1 [L]
The first set of rules works for property, enquire, contact and home.
If the url is not one of these, e.g. www.foo.com/about-us, I want it to call the file custompages?ID=about-us but with this code, it isn't working. I am quite new to using .htaccess files and I can't figure out what the issue is myself.
Your last rule is not correct. Change that to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /custompages.php?ID=$1 [L]
The following RewriteRule works correctly
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
until I place this one just right below
RewriteRule ^([a-z0-9]+)/$ /profile/company-profile.php?cname=$1 [NC,L]
Now every domain.com/something-here goes to the company-profile.php
How can I fix this ?
Firstly, the domain.com/login takes you to the index.php of the login folder.
When I type the second RewriteRule, if I write domain.com/login again it shows (in the background) the company-profile.php?cname=login
I would recommend to use this:
RewriteEngine On
RewriteBase /
# redirect company specific request
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9\-_]+)/?$ profile/company-profile.php?cname=$1 [NC,L]
# redirect all others
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/?$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]