Show a custom page for notfound folders - .htaccess

I want so show A custom page for example sub.php , whenever a url is not found
for example
http://domain.com/blabla/
http://domain.com/blabla/bladas.html
http://domain.com/tedabad/dasd/sadsad/
So above are all the examples of not found urls , Which I would like to display contents of http://domain.com/sub.php , I tried below code , but its not working
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 ^([^/]+)/?$ /sub.php [L]
RewriteRule ^([^/]+)/(.+?)/?$ /$1/sub.php [L]

You can check for presence of sub.php in a RewriteCond and use an ErrorDocument directive:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/sub\.php -f [NC]
RewriteRule ^([^/]+)/(.+?)/?$ /$1/sub.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /sub.php [L]

Related

how do I add pagination in htaccess

I want to get the number after the directory /blog/page/
I already have .htaccess for blog folder
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteBase /blog/
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [L,R=307]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
What I want is in /blog/page/<get any number here>
example:
www.example.com/blog/page/2
www.example.com/blog/page/3
I want to get the number after the /blog/page/

hide some part of the url using htaccess

I have the following htaccess file which works for my application:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
How to change urls like http://example.com/de/contact to
http://example.com/contact ?
I tried something like:
RewriteCond %{REQUEST_URI} !^de/
RewriteRule ^(.*)$ de/$1 [L]

url not rewriting i am using below code and mod_rewrite is also on

My url is like:
http://www.example.com/mock_profile.php?user_name=Yasin
but I want an seo friendly url like:
http://www.example.com/Yasin
I am using the below url RewriteRule in my httaccess but it is not working
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ mock_profile.php?user_name=Yasin
RewriteRule ^([a-zA-Z0-9_-]+)/$ mock_profile.php?user_name=Yasin
## Below is my all httacess code ##
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#RewriteRule ^(.*).html$ $1.php [QSA]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [NC,L,R=301]
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1
Try this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1 [L]
Explaination:
The following Condition tells the server to stop rewriting if the Request is for an existing directory.
RewriteCond %{REQUEST_FILENAME} !-d
And this tells the server to stop rewriting if the request is for a valid file:
RewriteCond %{REQUEST_FILENAME} !-f
If the request is not for a file /dir ,then rewrite it :
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1

php rewrite using htaccess

I need to rewrite my url
http://midogames.com/game.php?id=%D9%84%D8%B9%D8%A8%D8%A9%20%D8%B5%D8%A7%D8%A6%D8%AF%20%D8%A7%D9%84%D8%B0%D9%87%D8%A8
to
http://midogames.com/%D9%84%D8%B9%D8%A8%D8%A9%20%D8%B5%D8%A7%D8%A6%D8%AF%20%D8%A7%D9%84%D8%B0%D9%87%D8%A8
"please note they are spaces in the link"
i am doing this
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+) /game.php?id=$1 [NC,L]
</IfModule>
but it did not work with me, any suggetion
Try with NE flag:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+game\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ game.php?id=$1 [NE,L,QSA]

RewriteCond to catch URLs not ending with /

I'm trying to write a RewriteCond to my htaccess file to catch only URL's that DO NOT end with a / .
This is my file so far:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./wedding.php?w=$1
So I'm trying to pass the "W" GET var only if the URL does NOT end with /
So this will not be rewritten:
domain.com/bla/
But this will:
domain.com/bla
Please assist
Cheers!
Answer:
This is what I ended up using:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./wedding.php?w=$1
Edit For Howlin:
This is my full htaccess, I have wordpress and a web app living together on the same dir, so if the URL isnt for the webapp it will go to Wordpress:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./wedding.php?w=$1
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This should work for you:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]+)$ /wedding.php?w=$1 [R]
The above should only redirect urls that don't exist and don't have a / at the end.

Resources