Why htaccess isn't working with pretty links - .htaccess

Here's my current code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /go/redirect.php?slug=$1 [L]
This file is located at http://example.com/go/.htaccess
It is working correctly when people visit this link:
http://example.com/go/test
But not when it has a trailing slash like this:
http://example.com/go/test/
When the trailing slash exists, they are being redirected here for some reason:
http://example.com/test
How can I make this work with and without trailing slashes at the end of the URL?

Try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /go/redirect.php?slug=$1 [L]

Related

Use 2 urls for the same directory using htaccess

Is it possible to using htaccess?
I want to keep my original url "menu", but also use /en/menu/ for it.
Here is my code so far (sorry I am terrible at htaccess):
RewriteEngine On
RewriteBase /server/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/menu/ [NC]
RewriteRule ^([^/]+)/([^/]+)en/menu/ [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /menu/ [L,QSA]

How to automatically remove trailing slash in .htaccess file

I would like to remove trailing slash automatically. When i insert slash at the end of the URL the page leads to Google search.Here my .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*) index.php/$1 [L,QSA]
Kindly try the code below. Insert it below RewriteEngine On.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]`

htaccess rewriting to sef url

I want to rewrite my url from http://xyz.edu/profile.php?name=gavin to http://xyz.edu/gavin.html
Sample code:
RewriteRule ^profile\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ profile.php?compname=$1 [L]
Well it's about the same rules you already have. Try this and let me know how it works for you.
RewriteEngine On
RewriteRule ^profile\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.html$ profile.php?compname=$1 [L]

htaccess rewrite index.php on root and subfolders

I'm trying to redirect all requests to ./index.php?site=$1 while I only want to use the part behind the last slash.
So I want www.mydomain.com/firstpage to become www.mydomain.com/index.php?site=firstpage and www.mydomain.com/subfolder/anotherpage to become www.mydomain.com/subfolder/index.php?site=anotherpage
But right now www.mydomain.com/subfolder/anotherpage becomes
www.mydomain.com/index.php?site=subfolder/anotherpage
This is what I've got:
RewriteEngine on
Options +SymlinksIfOwnerMatch
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L]
What could I do to redirect only the part after the last slash?
Thank you so much!
SOLUTION:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*/)?([^/]+)$ $1index.php?site=$2 [L]
Found this other, on my opinion more elegant solution. By this, the number of subfolders is irrelevant.
SOLUTION:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*/)?([^/]+)$ $1index.php?site=$2 [L]
Finally figured out the answer myself.
At first I tried putting more RewriteRules under the same RewriteCond-lines gave me server errors. So I duplicated the RewriteCond-lines for the subdirectories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ $1/index.php?site=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L]

.htaccess : redirect everything except a few folders

i have this problem:
I have an .htaccess file witch rewrites all my url's to index.php
i have an exception for a few folders.
THis words fine, but when a file doesn't exist in one of this folders he still does the rewrite. But he may not doe that.
How can i fix this problem?
My code :
RewriteEngine On
RewriteRule ^(templates|images)/ - [L]
RewriteRule ^(favicon\.ico) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(templates|images|favicon\.ico) [NC]
RewriteRule .* index.php [L,NC]

Resources