RewriteRule for hiding file extension and change file name - .htaccess

starting url - example.com/vid.php
wanted url - example.com/video
any help, pls, to write the RewriteRule
RewriteCond %{THE_REQUEST} /vid\.php\s [NC]
RewriteRule ???

With your shown samples, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also make sure that your vid.php file is present alongside with htaccess rules file.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/vid\.php\s [NC]
RewriteRule ^ video? [R=301,L]
RewriteRule ^video/? vid.php [NC,L]

You don't even need a separate RequestCond here...
RewriteEngine on
RewriteRule ^/?vid\.php$ /video [QSA,R=301,L]
RewriteRule ^/?video/? /vid.php [QSA,END]
Test with a fresh anonymous browser window.
It is a good idea to start out with a R=302 temporary redirection and only change that to a R=301 permanent redirection once you are satisfied.

Related

301 redirect from URL with GET-parameters to the homepage

I have an old website with Joomla 1.5. It has some strange links with GET-parameters, like this:
http://www.primavista.ru/images/stories/catalog/?rand=1186511674
http://www.primavista.ru/images/stories/catalog/?rand=145388433
http://www.primavista.ru/images/stories/catalog/?rand=1553907057
http://www.primavista.ru/images/stories/catalog/?rand=1563973527
http://www.primavista.ru/images/stories/catalog/?rand=1981273478
http://www.primavista.ru/images/stories/catalog/?rand=2139631800
http://www.primavista.ru/images/stories/catalog/?rand=366928750
http://www.primavista.ru/images/stories/catalog/?rand=524689684
http://www.primavista.ru/images/stories/catalog/?rand=569077423
http://www.primavista.ru/images/stories/catalog/?rand=573405687
http://www.primavista.ru/images/stories/catalog/?rand=879649167
I want make redirect theses links to the homepage.
I tried some different instructions in .htaccess:
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=([0-9]*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} ^/images/stories/catalog/?rand=(.*)$
RewriteRule ^(.*)$ https://primavista.ru/? [R=301,L]
RewriteCond %{QUERY_STRING} (^|&)(rand)=[^&]+ [NC]
RewriteRule ^images/stories/catalog(/.*)?$ https://primavista.ru/? [R=301,L,NC]
But no one not working. Maybe here someone can help me with this. Thanks
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ / [R=301,L]
It is a good idea to start out with R=302 temporary redirections and to only change that to R=301 permanent redirections one you are satisfied with everything. That prevents nasty caching issues on the client side ...
UPDATE:
Your comment below indicates that you actually ask to remove the GET parameter in the redirected request, which you never mentioned before...
You can use the additional QSD flag for that:
RewriteEngine on
RewriteCond %{QUERY_STRING} rand=\d+
RewriteRule ^/?images/stories/catalog/?$ /? [R=301,QSD,L]

Rewrite htaccess url and remove extension

I'm trying to rewrite and remove extension
http://www.example.com/pages/cart.php
to
http://www.example.com/cart
My htaccess file is located in the public folder.
RewriteEngine On
RewriteRule (.*) pages/$1 [L]
With your shown attempts, please try following htaccess Rules. Make sure you keep your htaccess file along with pages folder(not inside it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/pages/(cart)\.php\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^(.*)/?$ pages/$1.php [L]

How can i redirect /?lang=en|ru to /?lang=en#googtrans(en|ru)?

How can I add the Google translate parameter #googtrans(en|de) or other language, so the translation happens automatically?
Basically, when the user goes to https://example.com/page/?lang=de they are redirected to https://example.com/page/?lang=en#googtrans(en|de)
I use this .htaccess rule, but it's not working:
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^/?lang=en#googtrans(en|[a-z]{2}) [R=301,L]
EDIT: Adding edited Rules here.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/page/?\?lang=([a-z]{2})\s [NC]
RewriteRule ^ page/?lang=%1#googtrans(%1) [R=301,L,NE]
With your shown samples(this is considering that you are hitting URL like: https://example.com/page/?lang=de in browser), please try following .htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$ [NC]
RewriteRule ^page/?$ page/?lang=%1#googtrans(%1) [R=301,L,NE]

Why are is my homepage not redirecting?

Read a ton on creating the proper redirects, but still having an issue.
Moving a site from one domain unto another. The directory structure has changed as well. The subpages are working great, but the home page isn't. Here the code that I'm using:
RewriteEngine on
//301 Redirect Old File
RewriteRule oldsite.net newsite.com [R=301,L]
RewriteRule ^p_gallery.* http://www.newsite.com/gallery [R=301,L]
RewriteRule ^p_purchase.* http://www.shop.newsite.com [R=301,L]
Thanks!
Make sure you have enabled mod_rewrite.
Because your first rule is wrong:
RewriteRule oldsite.net newsite.com [R=301,L]
You can only match REQUEST_URI in the RewriteRule.
It should be like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.net$ [NC]
RewriteRule ^ http://newsite.com%{REQUEST_URI} [R=301,L]

.htaccess mod_rewrite won't skip RewriteRule with [S]

As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php
First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps
Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]

Resources