RewriteCond to catch URLs not ending with / - .htaccess

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.

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]

Show a custom page for notfound folders

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]

How to change URL structure with htaccess?

I have downloaded and installed http://www.question2answer.org/ on my website on http://www.yourstartups.com/discussion/ (discussion sub-directory)
Current url structure is like: example.com/index.php?qa=123&qa_1=why-do-birds-sing
I want to change to /123/why-do-birds-sing
(why-do-birds-sing is example query)
Following is my htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
Can anyone help me?
You need a new rule to rewrite your pretty URL.
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?qa=$1&qa_1=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0 [L,QSA]
</IfModule>

htcaccess external redirect is not excluding URI

I'm trying to exclude few URI from htaccess based redirection
My htaccess directives are bellow
<IfModule mod_rewrite.c>
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$ [NC]
RewriteRule !=/test$ http://domain2.com [L,R=301,NC]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
What wrong in this? Any suggesion?
I have tried in a different way as well
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/test
RewriteRule (.*) http://domain2.com? [R=301,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
While doing external redirection, exclusion of the the URI, test is not in effect. If I'm doing an internal rewrite, its working fine.
Maybe this works:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !/test
# Add next line
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule (.*) http://domain2.com? [R=301,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

Resources