.htaccess Conflict: How can I remove .php AND rewrite URLs? - php

Here's my situation: I have a directory which I need to rewrite URLs in, so that ?page=hello can be accessed at either /hello or /hello/. I then have a sub-directory which needs .php extensions to be internally appended, so /subdir/example.php can be accessed at /subdir/example and /subdir/example/.
I developed the two parts of the site independently, and I'm now having trouble getting htaccess to meet both requirements.
My current .htaccess file is:
RewriteEngine On
# The root directory bit...
Redirect 301 /home /
RewriteEngine On
RewriteCond %{REQUEST_URI} !/subdir
RewriteRule ^([A-Za-z0-9-_/]+)/?$ index.php?page=$1 [L]
# Remove .php
RewriteEngine On
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
The rules work perfectly independently, ie if I remove the other one, but, together, does append.php but the rewrite rule fails.
I've tried making two separate htaccess files, one for the subdir and one for the root dir, with the appropriate rule (nice URL rule for root dir and remove .php for subdir), but this just means that appending .php no longer works.
Does anyone have any solutions to this?- or any resources to point me in the right direction? I'm currently in a hole of PHP and Apache documentation!

This is what worked:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(subdir/.*?)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L]
This does only remove .php for files of the subdirectory, but is certainly better than nothing.

Related

htaccess Rewrite directory to php pages

Say I have these pages stored in the root:
page1.php
page2.php
how do I rewrite this:
domain.com/page1/
to:
domain.com/page1.php
P.S.Also need to make sure the rewritten url does not clash with a real directory.
UPDATE:
Solutions below were only working on my remote server, but not localhost Xampp server.
I added this first line to my htaccess and solutions below worked on my local server!
Options +FollowSymLinks +MultiViews
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can use the following Rule in /root/.htaccess :
RewriteEngine On
#skip real files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite /file/ to /file.php if /file/ exists as .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [L]
If you just want to redirect a small number of pages, you could do this:
RewriteRule ^page1/?$ /page1.php [NC,L]
RewriteRule ^page2/?$ /page2.php [NC,L]
You might want to consider something like the following, though, where you pass the number as an argument (assuming you are following a naming convention as you described).
^page/([0-9]+)/?$ /pageLoader.php?p=$1 [NC,L]

Why does my rewrite rule only work on specific word?

I need a rewrite rule, which rewrites every request to index.php, except when the word web is in the url. So, every image, JS, CSS file is located under the web folder. Here are my .htaccess file which currently does the trick:
<IfModule mod_rewrite.c>
RewriteEngine On
# Folders / files to exclude from rewrite divided by Pipe goes here:
RewriteRule (^|/)web(/|$) - [L,NC]
# turn empty requests into requests for "index.php",
# keeping the query string intact
RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon.ico$
RewriteRule ^(.+)$ index.php [QSA,L]
RewriteRule ^(.+)$ index.php [QSA,L]
</IfModule>
This works for:
http://localhost/~alpham8/application/index/index/
http://localhost/~alpham8/application/
http://localhost/~alpham8/application/index/someaction/
http://localhost/~alpham8/application/web/img/someimage.png
But it does not work for:
http://localhost/~alpham8/application/somecontroller/someaction/
The none-working URL´s returns me an 404 error.
Please help me.

htaccess mod-rewrite: link to subdirectory from root - neglect dirs in subdirectory?

I asked part of this question a while ago on stackoverflow and this works perfect:
es
I have a multi-domain-server each directory is correspondend to a domain-name. However my main-domain name www.mydomain.com is also a directory on my server and not linked to automatically.
This is why I use this in my root of the website …
root/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
Now when I call www.my-website.com it automatically runs the /my-website directory. That's just fine. However in this directory I have another .htaccess file with this in it.
root/my-website/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^/jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(/downloads/.+)/?$ assets/$1 [L,NC]
So this means that for this website some links like /projects/ are just "neglected" and don't actually exist.
However if I run this website now live on my server and due to the main-htaccess file in my root, the RewriteConditions in my sub-directory don't work and I get a "Not found"
How can I make this work together?
Have it this way:
DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^projects/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^jobs/(.+?\.php)$ /$1 [L,NC]
RewriteRule ^(downloads/.+)/?$ assets/$1 [L,NC]
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_URI} !^/my-website/
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/my-website%{REQUEST_URI} -d
RewriteRule ^(.*)$ /my-website/$1 [L]
/my-website/.htaccess:
RewriteEngine On
RewriteBase /my-website/
# add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/my-website/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I was posting this on serverfault, when question was deleted. You already have a working answer, but I thought I'll still post this here, so you can have some explanations about this:
When you have mod_rewrite instructions in a .htaccess in a subfolder, only those instructions will apply, those of the parent folder will not be applied. You can make the rules that are in the .htaccess of the parent folder apply by using the
RewriteOptions Inherit
directive in the subfolder. But then it could become a bit messy. And you need to remember that the rules in the parent folder are applied after the rewrite rules in the subfolder. Better you go with a solution like proposed in only 1 .htaccess file.
Your 3 last RewriteRule search for a "/" at the beginning, which they never have in a per directory context, but have in a per server context. So if you are in .htaccess file (per dir context) then the comparison is made with a string that has no / at the beginning. See the doc about the RewriteRule.

Wordpress .htaccess file with current system

Okay, so I'll explain this to the best of my abilities. I have Wordpress files running from a separate directory "_esSite" in the root folder. To do this, I must update my .htaccess file with some code from Wordpress. HOWEVER, the code in the .htaccess file from Wordpress is now not allowing my real directories open properly because it tries to run it through Wordpresse's index.php. Basically, if the folder/directory actually exist on the server, I want it to ignore the Wordpress code in the .htaccess file. Hope that makes sense.
Example case:
If I have a folder on my server named "/xyz/", then I want the htaccess file to basically read everything ABOVE "# BEGIN WordPress" in the htaccess file.
If I enter "/abc/" and the folder doesn't exist, I want it to read everything BELOW the "# BEGIN WordPress".
Please let me know if you have any questions and I'll try to clarify. Below is my current htaccess file. Thank you very much!
DirectorySlash Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ _client.php?var1=$1 [L,QSA]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /_esSite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /_esSite/index.php [L]
</IfModule>
# END WordPress

Rewrite URLs without file extension (including across sub-directories)

I have the following .htaccess file on my server (It's a tweak of code I found on-line somewhere, for the life of me I cannot remember where) and I'm not quite sure how to amend it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
It basically grabs the URL, removes any trailing /, adds .php to the end of it, goes to that page, but then displays the link with a / instead of the extension in the URL bar (from what I can tell).
It works really well, bar two annoying niggles:
I can then only have .php files within that directory. Any others break.
The .htaccess file needs to be copied to EVERY subdirectory to function.
Can this script be improved upon at all, to do the following:
Put it in my root (/) directory and have it work in to all sub-folders?
Allow multiple file extensions within each directory? (e.g. some .php, some .html)
This code should work from your DocumentRoot and will add all the extensions:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.+)$ /$1/ [R=301,L]

Categories