Two .htaccess rules not working with each other - php

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]

Related

How to use .htaccess rewrite to completely rewrite the URL

I'm trying to imitate a routing effect. In my setup the login.php file is located under localhost/session/login.php. I've already found out how to omit the .php file ending but that's not enough for me. Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This changes the URL to localhost/session/login but what I really need it an URL like this: localhost/login.. The trouble is that I don't really understand how the rewrite above works and I'm getting a lot of mixed answers.. don't know what to do exactly.
Is there a way to have my URL be localhost/login by using some RewriteCondition?
You may use following rules in your site domain .htaccess to enable use example.com/login rewriting to example.com/session/login.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/session/$1.php -f
RewriteRule ^(.+?)/?$ session/$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/?$ $1.php [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess.
Make sure to put this .htaccess in DOCUMENT_ROOT directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /session/$1 [L]
</IfModule>
Flags used are,
L - Last
This will serve the file at location localhost/session/login.php to localhost/login.php

.htaccess exclude images from rewrite condition

I've been trying to exclude png and jpg files from being excluded from the rewrite condition. Currently, I have a set of pictures that are being presented as broken images. What is causing the error is a rewrite rule in my .htaccess. That rule overwrites the URL of a folder so it will appear with a different name in the URL, instead of /volley it appears as JeepVolleyballChampionship. I've tried to get rid off the last part that redirects to the home page if a directory doesn't exist, but nothing seems to work. I've also tried to include an exclusion of files with extensions of png and jpg from the rewrite block that rewrites the directory...
What I assume is that I'm not implementing that exclusion correctly. My page still loads fine, except with the broken images. Can you provide insight of what I'm doing wrong in this block?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
My whole .htaccess is provided here:
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
DirectorySlash Off
RewriteEngine on
#try to make it so that /volley/ is eliminated from folders and name changes but it is still under this structure
RewriteRule ^volley$ /volley/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/$ /volley [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/index.php$ /volley [R=301,L]
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
#non-existent folder to default root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
jpg and jpeg are treated as separate extensions here. Add jpeg as well on the line no 3. It may fix your issue, if your extension is "jpeg".
It will look as follows:
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]
You should not be entering "RewriteEngine on" multiple times, and you also need to be careful about the order of your rules. Off the top of my head I think all you should need is this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/volley /JeepVolleyballChampionship [R=301]
The !-f condition will prevent the images from being rewritten (along with all other files) so long as they exist, this is the canonical way to exclude all existing files like js, css, etc.
Then the pattern in the rule will only rewrite URLs that start with /volley, so you do not need a condition to match it -- in fact, the rule pattern is checked BEFORE the conditions, so matching there is faster than matching everything and then checking the conditions.

.htaccess file doesn't work properly with path_info and/or get method

Fixed it. Did a small change on the file. It can be seen below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [NC,QSA,L]
I have been trying for a whole day to fix an issue I'm having. I have this .htaccess file which simply rewrites the urls just like
http://example.com/about/?req=true
http://example.com/index.php/about/?req=true
It works on my localhost without a problem, but when I upload it to the server I get this "no input file specified" error. You can see the content of the .htaccess file below.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php/$1 [L]
I tried different things I found on the internet including adding a php.ini file which sets cgi.fix_pathinfo to 1, but none of them worked except one and that one doesn't read the request string. You can see that one below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ /index.php?x=$1 [L]
Thanks in advance.
First off all check whether rewrite module is enable in apache or not
if enable then u can use this code hide index.php with in url
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /base_directory_name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /base_directory_name/index.php [L]
</IfModule>
I think this will help u .

htaccess not working cant find file

RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Download/(.*)/?$ ./downloadit.php?key=$1
RewriteRule ^Buy/(.*)/?$ ./buy.php?key=$1
RewriteRule ^Admin/(.*)/?$ ./admin.php?cmd=$1&key=$2
RewriteRule ^Admin/Edit/(.*)/?$ ./editfile.php?key=$1
I dont normally work with Apaches Rewrite Engine so Im not sure.
The Link I am Attempting to Parse is: http://Website.com/~User/Download/Testlicious/
I have 4 Files in total, all of them php files. I will like to rewrite directly to those files
But it keeps giving me a 404 cannot find /downloadit.php its in the same directory, same with the rest.
*UPDATE***
Since on on a Virtual Host, Duh. I need to Set my Rewrite Base Correctly, DOH
RewriteEngine On
RewriteBase /~GameCenter/
RewriteRule ^Download/(.*)/?$ ./downloadit.php?key=$1 [qsa]
RewriteRule ^Buy/(.*)/?$ /buy.php?key=$1
RewriteRule ^Admin/(.*)/?$ /admin.php?cmd=$1&key=$2
RewriteRule ^Admin/Edit/(.*)/?$ /editfile.php?key=$1
Now everything now Links and Works but the Problem now is that $0/$1/$2 dont trigger anything in the Files. Nothing Spits out in $_POST['key']
Now I guess Step 1 of the Problem is Solved. But now onto O_O STEP 2!!
You need to add the conditions in for each rule, and I'm pretty sure the RewriteRule pattern match requires a slash at the start too.
I've assumed the 4 PHP files are in the same directory that your .htaccess file is in.
The [L] tells mod_rewrite to stop parsing the .htaccess file for the current request:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Download/(.*)/?$ /downloadit.php?key=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Buy/(.*)/?$ /buy.php?key=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# You've only specified one matching group here, so $2 will be blank
RewriteRule ^Admin/(.*)/?$ /admin.php?cmd=$1&key=$2 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^Admin/Edit/(.*)/?$ /editfile.php?key=$1 [L]

TinyMCE conflicting with htaccess

I have configured my server using htaccess so that I can route all URL's into a PHP $_GET parameter. I want the URLs on the site to finish with .htm. Currently I have this htaccess, and I have never had any problems with it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ajax/([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1&opformat=ajax [QSA]
RewriteRule ^([A-Z,a-z,0-9,\_/]*).htm$ index.php?route=$1 [QSA]
However I have just installed TinyMCE which uses .htm files in the js folder e.g. "/js/tinymce/prop.htm". For some reason when this URL is entered my htaccess routes it to php and it doesn't work.
I was given to understand that RewriteCond %{REQUEST_FILENAME} !-f should stop this from happening. Why is it not? RewriteCond %{REQUEST_FILENAME} !-d is working as I can go to "/js/tinymce" in this example and see directory listing.
Thanks.
I suggest you to change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^ajax/([^.]*)\.htm$ index.php?route=$1&opformat=ajax [L,QSA,NC]
RewriteRule ^([^.]*)\.htm$ index.php?route=$1 [L,QSA,NC]

Categories