Remove URL file extension but retain file structure? - php

I have a URL like http://example.com/dev/testSite/ where the root of the testSite is in the dev directory. I have that directory set up like:
testSite
-images/
--image.png
-css/
--style.css
header.php
footer.php
index.php
page.php
I have also included in my .htaccess:
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]
The first issue with this is instead of going to page.php like http://example.com/dev/testSite/page/ it is instead redirecting to http://example.com/page/. Eventually this site will live at http://testSite.com so I assume this code is going to work correctly. In the meantime, is there any fast I can do to make it work in the dev environment? Something that I can change quickly when moving it to the production environment.
The second issue is that this is also altering the file structure of the files. For example, any code on page.php like <img src="images/image.png"> is actually trying to find this image in http://example.com/dev/testSite/page/images/image.png. I suppose this .htaccess is making it behave as if I added the directory "page" instead of just hiding the extension.
There is no kind of framework or anything on the site so just regular php pages.

Your .htaccess should be like this in /dev/ sub-directory:
RewriteEngine On
RewriteBase /dev/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ $1/ [R=301,L]
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteRule ^([^/]+)/([^/]+)/$ $1/$2.php [L]
Then to fix relative paths in css/js you can add this in the <head> section of your page's HTML:
<base href="/dev/testSite/" />

Related

.htaccess file is working but causes constant refreshing/redirects

I have changed my .htaccess based on a tutorial to hide the .php file extension and $_Get from URL but the page keeps on jumping.
I have tried a number of .htaccess changes this is the only one that is even partially working. However, it seems to be causing constant redirects/refreshes.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/([0-9a-zA-Z_-]+) $1.php?name=$2 [NC,L]
Looking to change "profile.php?name=company-name" to "profile/company-name" without the page looking or functioning any differently. Some of the $_Get dynamic content is loading but the entire page keeps on skipping and none of the CSS styles are working.
Replace all of your rules with this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP:Host}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([\w-]+)/?$ $1.php?name=$2 [QSA,L]
Make sure to test this change in a new browser to avoid old browser cache.
For your css/js/issues add this just below <head> tag of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

HTAccess Rewrite Issues

I am writing a website which is dynamically populated through an Oracle database.
I have completed the desktop site and am now required to create a mobile site. Due to how different the sites are planned to look, I have opted to create 2 different "template" like websites for the mobile and desktop sites.
However, for my desktop site, everything is built off the index.php file in order to allow it to be completely dynamic. Pages are therefore look like www.domain.com/index.php/page in the url.
For the desktop site, this works. I am using a generic index.php removal rewrite rule in order to then make the url www.domain.com/page however still display the same page as the previous URL.
My issue, is that now I have a www.domain.com/mobile/index.php. Which has been created and for the most part has been working, however when trying to add addition dynamic pages to the mobile site. www.domain.com/mobile/index.php/about for example just redirects to www.domain.com/mobile/ and it doesn't even include the about part of the URL.
After much debugging, I have discovered it is definitely the .htaccess that is causing the issue.
If you have any insight into my issue, please help me out.
Thanks in advance
EDIT 1
Rewrite Rules are as follows
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
You can use this code in your /mobile/.htaccess file:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /mobile/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(?:/(.*))?$ $1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
This will override all the rules present in parent .htaccess for /mobile/ URI path.
Simplified version to make it work in root .htaccess:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(mobile)/(.*)$ $1/index.php/$2 [L,NC]
# Directs all EE web requests through the site index file
RewriteRule ^(.*)$ /index.php/$1 [L]
Based on the debugging you mentioned, there is a rule in your .htaccess which is rewriting www.domain.com/mobile/index.php/about to www.domain.com/mobile/. So, if you find which rule this is, you can add one above it that will catch requested URLs for your mobile pages and then not allow the problematic following rule to run. Something like this:
RewriteRule ^mobile/index.php/([a-zA-Z0-9-_]+)$ ^mobile/index.php/$1 [L,QSA]
The L ensures that if the user's request matches this rule, no further rules (including the one causing the issue) will be executed.
Thank you for the answers you've both given, however neither of them worked, I've now solved the issue, it was to do with the final RewriteRule at the end
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I needed to change it to
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/mobile/.* [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /mobile/.* [NC]
RewriteRule ^(.*)$ mobile/index.php/$1 [L]
So that it would work with both mobile and desktop sites.

htaccess - multiple domain to subfolders

i'm try to do this thing:
I want to redirect all the traffic for a specific domain A to a subfolder of the root. I will do the same thing with multiple domains with their subfolder.
Example:
www.domaina.it -> /public_html/domainadir/
www.unioncucine.it -> /public_html/unioncucine/
For that i have done it using this code that seems to work:
DirectoryIndex index.php index.html
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/unioncucine/
RewriteCond %{HTTP_HOST} ^(www\.)?unioncucine\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /unioncucine/$1
RewriteCond %{HTTP_HOST} ^(www.)?unioncucine.it$
RewriteRule ^(/)?$ /unioncucine/index.html [L]
It seems to work but not in this case:
I want to access www.unioncucine.it/web in which i have loaded a test joomla page (it has not template because i'm working on it).
If you go here: www.unioncucine.it/web i'm redirected to www.unioncucine.it/unioncucine/web/ and it does not load properly contents.
Going here: www.unioncucine.it/web/ it works fine. (The page is just some words and a css scretch).
#Changing
RewriteRule ^(.*)$ /unioncucine/$1
#To
RewriteRule ^(.*)$ /unioncucine/$1/
It seems to work without redirect but all the files inside other folders are not available.
Thank you
Edited:
Changed as you told:
RewriteCond %{REQUEST_URI} !^/web/templates/protostar/ [NC]
RewriteRule ((?:css|js|images)/.+)$ /web/templates/protostar/$1 [L,NC,R=301]
RewriteCond %{REQUEST_URI} !^/unioncucine/
RewriteCond %{HTTP_HOST} ^(www\.)?unioncucine\.
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /unioncucine/$1/
RewriteCond %{HTTP_HOST} ^(www.)?unioncucine.it$
RewriteRule ^(/)?$ /unioncucine/index.html [L]
But it still don't work: http://www.unioncucine.it/css/template.css
You're getting redirected from www.domain.it/domainadir/test to www.domain.it/domainadir/test/ due to mod_dir which adds a trailing slash after a directory.
Add these 2 lines on top of your .htaccess before other existing code:
DirectoryIndex index.html
DirectorySlash Off
Though you should add rule to add trailing slash as well in your rule as:
RewriteEngine On
# fix css/js/images
RewriteCond %{REQUEST_URI} !^/web/templates/protostar/ [NC]
RewriteRule ((?:css|js|images)/.+)$ /web/templates/protostar/$1 [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/domainadir/
RewriteCond %{HTTP_HOST} ^(www\.)?domaina\.
RewriteRule ^(.*)$ /domainadir/$1/ [L]
For css/js/images better to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's header:
<base href="/" />

Mod Rewrite load index.php on bad path

These are the rewrite rules I'm using. At this point I'm trying to force bad urls to load index.php. The html tag <base href="/directory-name/" /> is in the top of index.php, so my css, images and other assets are loading properly.
Options -Indexes +FollowSymLinks
RewriteEngine On
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]
RewriteRule ^(.*)/(.*)/(.*) /index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^(.*)/(.*) /index.php?x=$1&y=$2 [L]
RewriteRule ^(.*) /index.php?x=$1 [L]
Url Examples:
www.domain.com/directory-name/
www.domain.com/directory-name/index.php
www.domain.com/directory-name/index.php/arg1/arg2/arg3
www.domain.com/directory-name/anything-here
www.domain.com/directory-name/anything-here/arg1/arg2/arg3
The first 3 url examples work fine and load index.php. But the 4th and 5th urls fail and dumps me back to the html root / where a xampp index exists. I'm assuming the failure of the last url rewrite is because the index.php is in a sub-directory off of root?
How would I fix that? How would I force the urls to load www.domain.com/directory-name/index.php even if something else is in the path?
Try these rules:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2 [L]
RewriteRule ^([^/]+)/?$ index.php?x=$1 [L]

htaccess remove file ext for folder

I've tried looking at other tutorials and examples but nothing quite fits my scenario. My htaccess file looks like this:
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^view/([^/]+)/([^/]+)/?$ index.php?page=view&id=$1&title=$2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9]+)
RewriteRule ^([a-zA-Z0-9]+)$ /%1/? [R=301,L]
ErrorDocument 404 /404
I'm using PHP and need to rewrite /index.php?page=page&moreparamaters=1
But I also have a folder on the server called /admin/ and I want to remove the .php extension from the files in this folder so that:
/admin/lol.php becomes /admin/lol/
I have lots of these files so I can't really add each one in, individually. How can I do this without affecting my index.php rewrites?
Thanks
you have to think the other way round. Assuming that all your files are placed in the folder directly (meaning, no subfolders), this will work:
RewriteRule ^admin/([^\/]*)? /admin/$1.php [L,R]
In your links you may then write /admin/lol/ or /admin/lol, both will be redirected to /admin/lol.php

Categories