I have htaccess in my PHP website. I want to work url with or without trailing slash.
Below code works only with the trailing slash.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/$ $1.php [NC,L]
e.g. www.mydomain.com/index/ --Works
www.mydomain.com/index --Not Work
If anybody know how can I get this without trailing slash?
Thanks
You have this rule:
RewriteRule ^(.*)/$ $1.php [NC,L]
If you want to match it regardless of / in the end use:
ErrorDocument 404 /not-found.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f will stop reapplication of same rule since $1.php will be a real/valid file.
Put a ? after the / to make it optional.
Fix would be
RewriteRule ^(.*)/?$ $1.php [NC,L]
This is what I did with something similar
RewriteRule ^(.*)/?$ /$1.php [NC,L]
Related
I have a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And I have a file called random.php.
All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.
EDIT 1: When I use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]
then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.
Try this rule with optional trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You almost had it right, you just needed to exclude the slash as well:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.\/]+)$ $1.php [NC,L]
I have issue on url rewrite i am want to change url
http://www.example.com/mbl.php?domain=example.com to http://www.example.com/mbl/example.com
So i have written code myself by as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/mbl/([^/]+)$ /mbl.php?domain=$1
But its not working. so please to my error.
Thanks
You should try your rules this way and remove the leading slash from the rewriterule with mbl since you are using it in .htaccess file.
Give these rules a try.
#Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^mbl/([^/]+)$ /mbl.php?domain=$1 [L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
hi guys can you help me here im having a little trouble here im tring to use the htaccess to remove all 20% in my url and replacing it with hyphen I manage to get rid the other 20% in between the words Acer,Liquid,S1,S510
here is my url /localhost/gadgets/product/Acer-Liquid-S1-S510%20Mobile
As you can see there is one %20 in last part, how can I remove it
And here is my htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /gadgets/
Options -Indexes
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1-$2 [NC,L]
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Thanks in advance guys
Try this:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /gadgets/
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=Redirect:1]
RewriteCond {ENV:Redirect} ^1$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1 [NC,L]
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The first rule removes all the whitespaces in a loop (using [N]) so we don't have to specify multiple RewriteRules doing it one by one now.
The next rule (with {ENV:Redirect} condition) is optional and is used to reflect the use of hyphens on the client's browser as well so that any bookmarks created link to the correct non-whitespaced version of the URL.
Is that possible to hide the .PHP extension and redirect the URL at same time.
example : http://example.com/test need to redirect to http://someothersite.com
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)/test http://someothersite.com
But its not working.
Any idea ?
Thanks
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^test http://someothersite.com [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
The code above is not tested, but it should give you an idea about it. Use L flag after each set of rules, as it stops processing of the rest of the rules.
You need just this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ $1.php [R=301,L]
For cross domain rewriting:
RewriteRule ^test http://someothersite.com [R=301,L]
Allways use L = 301 for permanent redirectings.
I'm working in a directory called testsite and I want all .php extensions to be replaced by a trailing slash. I'm halfway there, in that (for example) entering http://mydomain.com/testsite/about means that http://mydomain.com/testsite/about.php is loaded.
However, I now want the URL to be displayed as ./about/ so that only one version shows up in search engine rankings.
Here's my .htaccess:
RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]
Also, is it possible to preserve (and hide from display any parameters that I pass)?
All help is much appreciated!
Try these rules:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]
Try replacing the last rule with:
RewriteRule (.*)$ $1.php [NC] [E=ORIGINAL_URL:$1]