.htaccess rewrites for specific directories - php

I've been assigned to a new website and I need to incorporate page routing and "vanity URLs" with .htaccess
I'm really not familiar with htaccess, and despite all my research efforts, I have not gotten any solid answers.
The website has a custom-built PHP admin system located in a subdirectory called "admin". I don't want to mess with it at all, so whatever changes I made to htaccess cannot affect that subdirectory.
So, given that the page has 3 main "pages": home, buy, and sell, I need requests made to "www.sitename.com" and "www.sitename.com/index.php" to route to the homepage. I need requests made to "www.sitename.com/sell" to route to the sell page.
And then the hardest part, I need requests made to "www.sitename.com/buy" to route to "buy.php", but requests made to "www.sitename.com/buy/category-name" to route to "www.sitename.com/buy.php?c=12"
I know its a lot to ask, but if anyone can guide me in the right direction, it would be much appreciated.

These rules should work for you:
RewriteEngine On
RewriteBase /
# skip admin OR any valid file/dir
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} /admin/ [NC]
RewriteRule ^ - [L]
RewriteRule ^(sell|buy)/?$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(buy)/([^/]+)/?$ $1.php?c=$2 [L,NC,QSA]

Try this:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^sell$ ./sell.php
RewriteRule ^buy$ ./buy.php
RewriteRule ^buy/(.*)$ ./buy.php?c=$1
If the category is always numeric replace the (.*) with (\d+)

I think this hope
/* copy and paste below, save the name. htaccess */
RewriteEngine On
RewriteBase /yousite.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
#ex1:http://yousite.com/buy/3
RewriteRule ([a-zA-Z_-]+)/([0-9]?) buy.php?c=$1
/* and in your file php */
<!-- #ex1:http://yousite.com/buy/3 -->
buy/3

Related

How to reach 404 page through multiple htaccess rewrites

I'm pretty novice at htaccess but I know enough to make a few simple redirects and rewrites happen. My problem is that I've never combined multiple rewrites, such as in the custom PHP/MySQL gallery system I've created, and so I'm having a few issues.
The main things I want to accomplish are to have the gallery system structured as such:
https://example.com/album
https://example.com/gallery
https://example.com/gallery/img/img-name
This is actually working successfully already, but it seems to also be catching any invalid URLs and assuming they're gallery names, so my 404 page goes completely overlooked. So for example,
https://example.com/asdf
brings up a blank gallery page when it should obviously produce the 404. Do I need to somehow have my htaccess file check my database for potential existing name conflicts? Is that possible? Or is there a simpler solution that I'm overlooking?
Here's what I have now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /album.php?a_id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /galleries.php?g_id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z-]+)/img/([a-z0-9-]+)/?$ img.php?g_id=$1&i_id=$2 [NC]
Options -Indexes
ErrorDocument 404 /404.php
Thank you in advance!

How to make dynamic directories and subdirectories via .htaccess

I am trying to create an htaccess script that will create directories for either zero, one, or two variables.
My file that is being used accepts the following 2 get variables. make and model.
This is a 3 step page. I currently have the page located at at /new.php. This page allows a user to select the first variable (in my case, a vehicle make). By selecting a make on this page the user is taken to /new.php?make=Acura. This page now displays a list of all Acura models. From here, the user can click a model, and they will be directed to /new.php?make=Acura&model=TLX. They can now choose a submodel and will be taken to an information page.
So I'm trying to get:
new.php to go to /new/
new.php?make=XMake to go to /new/XMake/
and new.php?make=XMake&model=XModel to go to /new/XMake/XModel/
This is as far as I have gotten with my code:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^new/(.*)$ new.php?make=$1 [L,NC]
However any variables I add after this seem to break the first directory? Why is this?
You can use these rules in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^new/([\w-]+)/([\w-]+)/?$ new.php?make=$1&model=$2 [L,NC,QSA]
RewriteRule ^new/([\w-]+)/?$ new.php?make=$1 [L,NC,QSA]
RewriteRule ^new/?$ new.php [L,NC]
The order of the rules is important. With this rule at the beginning, any request /new/, /new/XMake or /new/XMake/XModel/ matches, and the following rules are ignored.
To match the other rules, the more specific must come first, e.g.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+?)/(.+)$ new.php?make=$1&model=$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/(.+)$ new.php?make=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new/$ new.php [L,NC]

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.

Two .htaccess rules not working with each other

I have the following .htaccess:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]
But I would also like to remove .php extension with the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I have tried a dozen combinations based on answers elsewhere not just on StackOverflow but still cannot get it right, I either render pages not in shared directory unable to open with 500/404 errors or 500 error when I go to /shared.
After further investigation and trial when I add the rules to remove the .php extension it messes up the first rule to route anything under the path of /shared/ to shared.php the path /shared/username are not real locations but the script insures that the correct information is presented. It would be handy to ignore the second rule if the URL has /shared/ in the path? Is that possible? I am not rewriting everything to the /shared/ directory - only when the path reads /shared/username do I want that rule to kick in, everything else should be rewritten to the / base directory.
Keep your rules like this:
RewriteEngine On
RewriteBase /shared/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shared.php [L]

.htaccess file does not recognize index.php

I have created a .htaccess file that allows for vanity URLs. However, I am no longer able to type www.website.com without typing in the index file. I.e. I have to type in www.website.com/index.php in order to see the homepage. This is what my .htaccess file looks like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
Anyone know how to fix this? Thank you all!
The way you defined your rule increased the complexity.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
Above rule means if file name is a directory of file process as it is. after that dont process farther.
RewriteRule ^(.*)$ http://www.website.com/profile.php?u=$1 [NC]
This rule means map any request uri to profile.php?u=
Now when you request / that is www.website.com it checks the first rule and it fails to match. Then it check the second rule and maps it to profile.php?u=.
One way to fix it, would be check *if $_GET['u'] is empty or / in profile.php. If it is then load the index.php.
Another way is to find a proper regular expression for your user names once found use it here.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(USERNAME_REGEX)$ http://www.website.com/profile.php?u=$1 [NC,L]
The best way to handle this is using PHP,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?uri=$1 [L]
Now index.php will get every uri you pass. Now you can process the URI in index.php.
It may be due to your server set up.
Try DirectoryIndex index.php (see http://davidwalsh.name/directory-index-homepage-htaccess )
Edit (due to me not reading the question properly in the first place)
Have you tried it without the RewriteRule .* - [L] line?

Categories