index page in root folder and index page in subfolder error - php

I have site xyz.com with index.php page And there is a subfolder in rootfolder.
e.g. folder viz. "audio" which contains index.php and other pages.
Now problem is When I opened www.xyz.com/audio it opens but when i navigate in this audio folder through navigation menu and try to return to audio/index.php by clicking link in navigation bar in audio folder, it brings me to www.xyz.com/index.php instead of www.xyz.com/audio/index.php. But other pages navigation in audio folder works perfectly.
I have used .htaccess in root folder and in audio folder too with following rules (Used to remove file extension). Is it creating this error ? Or any other thing? I got stucked..
RewriteEngine On
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.(?:php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index [NC]
RewriteRule ^(.*?)index([/.]|$) /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]

Related

exclude php files or a whole directory from codeigniter

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)

How to define base URL in .htaccess

I am redirecting
https://www.example.com/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/<a number>/<the-page-title>
Now I want to move the website to a sub folder so the redirection will be
https://www,example.com/folder/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/folder/<a number>/<the-page-title>
I am using the below .htaccess code
RewriteEngine On
RewriteCond %{THE_REQUEST} /inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,NE,L]
RewriteRule ^(\S+)\s+(.*)$ $1-$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please Suggest.
thanks!
If you are using .htaccess in root directory you can add /foldername
RewriteCond %{THE_REQUEST} /folder/inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /foldername/%1/%2? [L,R]
To both rules
RewriteRule ^folder/([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please try above rules it I didn't tried it for now.

Remove file name extensions AND redirect from www to non-www with .htaccess

My domain name is https://example.com - I'm working with my .htaccess to remove the file extensions from the URL, so https://example.com/pages/file.php is getting displayed as https://example.com/pages/file, which is working fine.
I'm now having some problems with duplicate content and would like to redirect https://www.example.com/pages/file to https://example.com/pages/file. My current .htaccess looks like the following (it placed once in root directory and once the subdirectory /pages/):
ErrorDocument 404 /404.php
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/$2.php [L]
The problem is, by accessing https://www.example.com/pages/file, I'm getting redirected to https://example.com/file, so without the /pages/ part, which is very important as all subpages are stored into that directory. I don't know if it's important, but i got some header.php and footer.php files in my root directory which need to be accessable too.
Try to move this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/?$ /$1/$2.php [L]
To the top, just below these rules:
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
This should cause the urls with "/pages/" to be redirected correctly

htaccess Rewrite (remove php extension)

today I have a problem with my .htaccess file. On my server I have several folders, each of folders is separately website example below:
Server structure (global folder)
- my_portfolio
- website_two
- my_website
- example
In "example" folder I have that .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
#To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule working good when I open my website address is without "www" and .php extensions is invisible, but when I click on a link to other website in menu I get something like this:
www.example.com/example/contact.
Address to my website is www.example.com and everything for this website is inside example folder on a server. I dont know where or what I wrote wrong. Always when I try to go on to another webside rewrite engine give me a link without .php extension but add to folder name before link.
This is good example:
www.example.com/contact.php - normal link without htaccess
example.com/example(folder name where is a website)/contact - now
example.com/contact - should be
Reorder your rules and keep redirect rules before internal routing rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Also test this from a new browser to avoid old 301 cache.

.htaccess to remove file extension not working

I have public html folder where some files and folders are stored. Then I have created a Folder In It. That Folder Contains Some Files And A Folder "Admin" which containes files related to my admin panel to surf my site members data.
Now I used Following Code In .htaccess file in public html folder which removes all pages's php extension.But When I try To Log In To My Admin Panel, Log In Fails.
e.g. www.abc.com >> Here .htaccess file Is with following code.
It works For Files in www.abc.com and for files in www.abc.com/xyz/index.php and other files. But When Tried www.abc.com/xyz/admin/index.php For Login, This Page just get refreshed But Doesn't allowing successful login.
Code used in .htaccess is as follows :
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{THE_REQUEST} /index [NC]
RewriteRule ^(.*?)index([/.]|$) /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Try these rules that avoid redirecting for POST requests:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.(?:php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index [NC]
RewriteRule ^(.*?)index([/.]|$) /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1.html -f [NC]
RewriteRule ^(.+?)/?$ /$1.html [L]

Categories