HTAccess Rewrite Issues - php

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.

Related

CodeIgniter Multi Application htaccess issue

Before you tell me to go to CodeIgniter multi-application .htaccess problem, hear me out.
The reason I say that is because my problem seems to be nearly the exact same as this posters. My directory structure is:
application/
admin/
app/
..
.htaccess
admin.php
index.php
The htaccess rules are:
RewriteEngine On
RewriteBase /dev/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ admin\.php [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ admin\.php/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]
Essentially what I need is:
type in example.com/admin - the url keeps the admin folder, and redirect the user to admin.php
type in example.com/admin/controller/method - same as above
type in example.com/anything_else - the user gets directed to the app instead of admin (index.php)
In testing the person in the aforementioned post's htaccess, which mine are essentially copied from, in an online tester, the two rules involving URIs with 'admin' in it are not passed for some reason. I can only get admin (not ^admin$) to work as a pattern (according to the tester).
RewriteRule ^dev/admin(\/?) admin.php? [L,QSA]
Should do it for you. This will account for /admin, /admin/ and /admin/whatever/here. But for the latter, be aware that since you have QSA on it will append it to the destination URL like so: /admin.php?whatever/here.

.htaccess not rewriting the URL correctly for .php files, but OK for .html files

I'm trying to accomplish 2 things in my .htaccess:
Redirect all requests for (in example) www.blanklabs.com/boarddrive/faq, www.blanklabs.com/boarddrive/faq.htm, www.blanklabs.com/boarddrive/faq.html, or www.blanklabs.com/boarddrive/faq.php to www.blanklabs.com/boarddrive/faq.php
The browser's address bar should show just www.blanklabs.com/boarddrive/faq, without the extension.
Here is my current .htaccess:
RewriteEngine On
# -- new
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [L,QSA]
On the server I have faq.html (for now), but I also tried having both faq.html and faq.php. Eventually it'll just be faq.php.
The .htaccess is clearly incorrect, since if I go to www.blanklabs.com/boarddrive/faq.html I get the correct content (from faq.html), but if I go to www.blanklabs.com/boarddrive/faq.php I get a 500 error. This happens even if I have faq.php on the server.
Any ideas what I'm doing wrong? The no-extensions is secondary, the primary goal is to redirect all requests from html to php files.
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# skip POST requests
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html?)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)(\.html?)?$ /$1.php [L,QSA]
You need to redirect
/faq.htm
/faq.php
[using redirect directive]
to /faq
now just applying rewrite rule to this later condition [/faq] to
/faq.php
It should work.

.htaccess rewrites for specific directories

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

How to hide .html extension from the website url

I know this question has been asked before but does anyone know of a good way to hide .html extensions. I've tried many of codes & many of the answers from the https://stackoverflow.com/ but am not seeing the result. Thats y I ask u again
I've a static website and I wanted to remove the extension to clean my urls. I was working with static html files.
Prior to removing the extension, the url would read website.com/about.html.
With the extension removed, it would look like website.com/about. Hope that was clear.
I've a .htaccess file and I tried many of the codes but it dosen't work. Here are some codes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://%{HTTP_HOST}/ [R=301,L]
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteBase /
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.html [nc]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
however I am not seeing any results...:(
You have your rule reversed (the first one would work otherwise):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Your original rule was RewriteRule ^(.*)\.html$ $1 [L] which translates to "If the requested resource name ends with .html then issue a redirect to the browser telling it to ask us if we have anything at the same name without the HTML". This results in a 404 (since Apache doesn't have anything to serve for the .html'less resource).
By switching the .html to the destination RewriteRule ^(.*)$ $1.html [L] we change the rule to say "If the requested resource name is not a file or directory on disk then try to re-route the request (internally, don't tell the browser) as if it ended in .html".
Note that you do not need complex (by nature) RewriteRules for such task.
The mod_negotiation apache module, often enabled by default, provides such behavior with Multivews option.
If the server receives a request for /some/dir/foo and /some/dir/foo does not exist, then the server reads the directory looking for all files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements, and returns that document.
.i.e requesting for foo/bar will serve foo/bar if it us a directory and will seak for foo/bar.html, foo/bar.txt and such etc if not.
You just need to ensure this option is activated in your current context (a Directory for example)
Options +Multiviews
use this
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L,QSA]
Look at this post http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ I haven't tried it yet but the demonstration seems pretty convincing

Mod_rewrite failing to silently redirect

2 days of struggling and about a pound less of hair, I figure it's about time I ask for some help. I've recently migrated my project to a main sub-directory so that the structure is as follows:
-application/
-index.php
-signup/
-index.php
-signup_set.php
-css/
-js/
I've been trying to get mod_rewrite to do my work for me to exclude "application/" from all the urls. Here is where I got in my root .htaccess:
RewriteEngine On
#first round through, prepend 'application/' to request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !^application
RewriteRule ^(.*)$ application/$1 [L]
#second round through, if the new url is not directory or file, append .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]
I may have a terrible understanding of regex/mod_rewrite, but this actually seems to partly work. Partly. The weird part about this is that when I route to a "pretty url" (e.g. www/signup) the url in the top bar is physically rewritten to include the "application/" (e.g. www/application/signup). So somehow this "silent" rewriting process is getting pretty loud...Any suggestions? I realize I could change my root directory but that would require rewriting of code referencing css/js files. I'm more curious than anything, just trying my hand at some htaccess. Any help would be appreciated! Thanks StackOverflow, you rock.
P.S. I'm running Apache on Windows 7 and using a virtual host (in case any of that is relevant)
The rules below work for me running Apache on Windows 7 using a virtual host. All I changed was the second RewriteCond.
RewriteEngine On
# Append trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule ^(.*)$ /$1/ [L]
# First round through, prepend 'application/' to request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/application
RewriteRule ^(.*)$ application/$1 [L]
# Second round through, if the new url is not directory or file, append .php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteRule ^(.*)/$ $1.php [L]

Categories