Htaccess :Rewrite rule issue - .htaccess

I've following code in a .htaccess file. The file is in a subfolder, lets say /content/.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L]
RewriteRule ^([^/]+)(/([^/]+)?/?)$ content.php?category=$1&slug=$3 [L]
I'm expecting above code will rewrite,
mydomain.com/content/text1/text2/edit/my_password to mydomain.com/content/edit.php?category=text1&slug=text2&password=my_password
mydomain.com/content/text1/text2 to mydomain.com/content/content.php?category=text1&slug=text2
mydomain.com/content/text1 to mydomain.com/content/content.php?category=text1
mydomain.com/content/ to mydomain.com/content/content.php
The code rewrites above correctly, But it breaks the asset urls.
e.g. It rewrites http://mydomain.com/content/js/tms.js to something else, it is same for the css files. My assets folders are /img, /css, /js.
If i commented RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L] line it works for the assets
Any ideas how to fix this?

The condition RewriteCond %{REQUEST_FILENAME} -d means that "the request is for an existing directory". If you add a ! in front of the -d it changes it to "the request is not for an existing directory".
Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/edit)+/([^/]+?)/?$ edit.php?category=$1&slug=$2&password=$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+?)/?$ content.php?category=$1&slug=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+?)/?$ content.php?category=$1 [L]

Related

404 errors after url rewrite with .htaccess

I'm using url rewriting in my website.
I've these url:
works
example.com/_new/app/planning
example.com/_new/app/user_info
doesn't work (404)
example.com/_new/try
example.com/_new/features
Here my rules:
RewriteEngine On
# Removing extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# example.com/app/[module]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?param1=$1 [L]
# example.com/app/[module]/[action]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2 [L]
# example.com/app/[module]/[action]/[type]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3 [L]
# example.com/app/[module]/[action]/[type]/[id]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3&param4=$4 [L]
Here the file structure:
/_new/
/_new/try.php
/_new/login.php
/_new/app.php
The try.php and index.php should be shown as example.com/try or example.com/login.
The app.php loads module from another directory. This one should be:
example.com/app/module_name.
What's the problem ?
Thanks.
In htaccess, the base URL example.com will be skipped in the matching, and in your file, the first regex ([^\.]+) will match /_new/try and it tries to redirect you to a file /_new/try.php which does not exist, hence 404.
Make sure to test your regex before trying to use the URL, you can benefit from https://regex101.com/ to make your tests.

how htaccess redirect file not found contain special word to other file

I have links as sample: with "med_ " in the file name
http://www.xaluan.com/images/news/Image/2016/10/09/med_357fa10d94d29d.img.png
I need to redirect it too the small file "small_" if it not found in server, and then if small_ file not found.. it will redirec to noimage.gif exiting on server
but my following code not work.. please help
RewriteCond %{REQUEST_URI} images/news/(.*?)med_(.*?)\.(.*?)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*$ /images/news/$1small_$2\.$3 [R]
thanks very much
udate: thanks I got answer from Croises bellow .. but need some bit more.. in case of small_ image not found too, what i need for the htacess make redirect to /noimage.gif
You can use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule images/news/(.*?)med_(.*?\.(?:jpg|gif|png))$ /images/news/$1small_$2 [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule \.(?:jpg|gif|png)$ /images/no_images.gif [NC,R,L]

htaccess remove parameter and index file

I'm trying to remove the proxy.php file and the parameter passed to it. This is what it looks like currently.
http://localhost/stuff/proxy.php?parameter=folder01/some.file
This is the way I would like it to look.
http://localhost/stuff/folder01/some.file
For my htaccess file, I have tried the following but they do not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /proxy.php?parameter=$1 [L]
And this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /proxy.php?parameter=$1 [L,QSA]
Any ideas?
Try it in root dir or put this in stuff directory and remove RewriteBase line.
RewriteEngine on
RewriteBase /stuff/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ proxy.php?parameter=$1 [L]

How to hide my .html and .php extensions using .htaccess?

I have this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.html
The problem is: Its just hidding .php
Shouldn't it hide .php and .html?
Change your rules to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
1./ your html files are still accessible because your second set of rule will never be used. Besides, your second line specifically tells the server to leave "real" files aside.
2./ You only need RewriteEngine On once .
3./ You should use the flags to tell your rewriting when to stop.
RewriteEngine On
# if the requested file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# if the requested folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# sends all urls except home to its corresponding php file
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]
But if you want it to work for all file extensions, remove the first rule, leaving only.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]

rewrite to another file if .php file not exists

I'm using the following rule to serve the associate .php file
All last directory pathname will go to the associate .php file
Ex. /about/ or /about will go /about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php`
So when I use http://example.com/abc it points to /abc.php, this is working fine. But if /abc.php file does not exist in that directory then I like to forward that to /listing.php file. How can I do that? please help
Also, I need this to apply to the root directory only. means
http://example.com/..*here*.. not for any subdirectory.
Thanks
Try this, this should be tested:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0\.php !-f //check if (for example) abc.php not exists then
RewriteRule ^(.*)$ /listing.php
RewriteRule ^(.*)$ $1.php
Update: I tested this on my local server, note that: the folder is test you can adjust for your own
Options All -Indexes
RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond test\/$0\.php !-f
RewriteRule ^(.*)$ listening.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

Resources