Folder and php file with the same name in website - .htaccess

How do i make it so this:
www.example.com/product
Will point to this file: /product.php but this:
www.example.com/product/thing.php
Will point to this directory: /product/thing.php
I have these rewrite rules in my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It's making this:
www.example.com/product.php
Into this:
www.example.com/product

Toggle the DirectorySlash to Off.
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

I found out you can do something else that is almost as good.
www.example.com/product
becomes:
www.example.com/product/
If you add a 'index.php' or 'index.html' in the product directory on the web server. So the path becomes product/index.php, and the url will look like this:
www.example.com/product/

Related

Point to a file in subfolder with .htaccess

I've wanted to move my index.php file from the root to /system/ folder, this is for no reason but giving myself opporunity to learn more about .htaccess, which I am finding very confusing.
How can I achieve this? Currently my code looks as follows
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?query=$1
This allows for example mydomain.com/some-url/ to become /index.php?query=some-url for example, this far I'm with it all. But moving the file into System folder and addind /system/ before /index.php does nothing. How does one do this? Thanks!
This could be done with a simple rewriterule. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ system/index.php [L]

.htaccess RewriteRule path to query string

I am trying to achieve that a url like
example.com/path1 to example.com/index.php?p1=path1
example.com/path1/path2 to example.com/index.php?p1=path1&p2=path2
In my .htaccess I have:
RewriteEngine On
RewriteRule ^(.*)/(.*)$ /index.php?c=$1&g=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9-_]+)$ /index.php?g=$1 [NC,L]
It works well for the case where we have the example.com/path1/path2 but for the the case example.com/path1 only works if there is no dot in the url. For example, I want to example.com/mydomain.com working to example.com/index.php?g=domain.com but I am not able to make it working.
Could you help me with this please?
here's how I would handle this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1
then with whatever serverside language you're using I'll use PHP parse $_GET['param'].
$params = explode('/', $_GET['params']);

Removing 2 directory and php from url

My current url is:
http://example.org/dir1/dir2/profile.php?username=someone
I want it to look like this:
http://example.com/someone
My .htaccess file is in public_html folder and it is completely empty.
Please, can you help me?
Try this :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ /dir1/dir2/profile.php?username=$1 [QSA,L,NC]
The conditions make sure you don't rewrite any existing file/directories on the server.

Trying to change stylesheet url

This is my current .htaccess file:
RewriteEngine On
RewriteBase /application/
RewriteRule (.*)/css/(.*).css www/$1/css/$2.css
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?p=$1
So my end goal here is to have
http://localhost/application/guestbook/css/style.css
forwarded to
/application/www/guestbook/css/style.css
It's almost working, yet when im dumping $_GET i can see that the url he's looking for is
www/www/guestbook/css/style.css
Can someone tell me why its having 2times www/ ? And how can I fix it?
Here is one way to do it:
UPDATED
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^.*/(\w+/\w+/\w+\.css)$
RewriteRule .* application/www/%1 [L]
Will redirect this:
http://localhost/application/anything1/anything2/anything3.css to
http://localhost/application/www/anything1/anything2/anything3.css

.htaccess redirection if file exist

I have a directory structure like this
/ub/
/ub/img/
/ub/inc/
Basically I want to change my img URL to it's parent directory (ub),
When i access http://localhost/ub/mypic.jpg
First, it has to check /ub/img/mypic.jpg, if exist then get that file and pass to the browser, if not then just pass it to http://localhost/ub/index.php?img=mypic.jpg with INTERNAL REDIRECTION.
Second, index.php will create mypic.jpg and store it to /ub/img/, then pass mypic.jpg to the browser.
So, whether the file is exist or not, the browser will only have http://localhost/ub/mypic.jpg as the URL
So far, i have this on my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/ub/img/$1 -f
RewriteRule .+ /ub/img/$1 [L]
I have 2 days to find out the solution, but, it doesn't seem to work,
Thanks,
EDIT :
Hey, I've got this, The request to http://localhost/ub/app/file.php is already passed to index.php?uri=app/file.php as i expected,
but the request to http://localhost/ub/img/mypic.jpg is not passed to index.php?uri=img/mypic.jpg. Is there any solution?
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/img/%1 -f
RewriteRule ^(.+)$ img/%1 [L]
RewriteCond %{REQUEST_URI} ([^/]+)$
RewriteCond %{DOCUMENT_ROOT}ub/inc/%1 -f
RewriteRule ^(.+)$ inc/%1 [L]
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
Try something like this (place it in .htaccess in the /ub folder):
Options +FollowSymLinks
RewriteEngine on
RewriteBase /ub/
RewriteCond %{REQUEST_URI} ([^/]+\.jpg)$
RewriteCond %{DOCUMENT_ROOT}/ub/img/%1 -f
RewriteRule .+ - [L]
RewriteRule ^(.*)$ index.php?img=$1

Resources