Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess rule to forward /login/ and /login to same page?

I have the following rule in my current .htaccess file to redirect /videos/login/ requests to /videos/login.php

RewriteRule login/ /videos/login.php

This works fine if I am access login page using http://mysite.com/videos/login/ but when I am trying to access the same page using http://mysite.com/videos/login (without ending slash) then it gives me "404 page not found" error.

Please tell me what will be the correct rule of .htaccess file so that any request for http://mysite.com/videos/login/ or http://mysite.com/videos/login will point to the same /videos/login.php page.

Thanks

like image 333
djmzfKnm Avatar asked Jul 09 '09 20:07

djmzfKnm


People also ask

How do I create a redirect rule in htaccess?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

What is rewrite rule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is QSA in htaccess?

QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite ( olle? p=1 will be rewritten as index.


2 Answers

Just make the trailing slash optional:

RewriteRule ^videos/login/?$ /videos/login.php

But you should better use just one variant (with or without trailing slash) and redirect one to the other:

# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]

# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]
like image 196
Gumbo Avatar answered Nov 15 '22 07:11

Gumbo


This work fine for me:

Rewriterule ^login(/|)$ /videos/login.php

like image 44
AOvejero Avatar answered Nov 15 '22 06:11

AOvejero