Creating an htaccess for .py files - .htaccess

How might I create an .htaccess so that it replaces any www.w.com/cgi-bin/x.py with www.w.com/x/
So basically, it replaces /cgi-bin/$x.py with /$x and adds a trailing slash to make it /$x/
Thanks!

You can use:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+cgi-bin/([^.]+)\.py [NC]
RewriteRule ^ /%1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ /cgi-bin/$1.py [L]

Related

remove inside url some element via htaccess

Via an htaccess, I want to remove this point inside url : Products/Description
example : https://test.com/Products/Description/logitech/Id-12
I tried this but it does not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
below is my complete htaccess tried rules file:
RewriteEngine On
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Remove Products/Description ===> not working
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
With your shown samples and efforts, please try following .htaccess rules file. We need to do few changes in it. Make sure that your .htaccess file is residing along with Products folder(not inside it, besides it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /Products/Description/
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
RewriteCond %{THE_REQUEST} \s/index\.php\?([^&]*)&([^&]*)&Id=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Products)/(Description)/(\d+)/?$ index.php?$1&$2&Id=$3 [QSA,NC,L]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Why htaccess isn't working with pretty links

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]

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 file extension hiding and pass url string

I am trying to hide .php extension and also rewriting the url string with trailing slashes. File extension hiding is working fine but url string is not.
From:
http://www.example.com/abc.php?id1=1&id2=2
To:
http://www.example.com/abc/id1/1/id2/2
Following is my .htaccess
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)\/([0-9]+)$ $1.php?id=$2&%1 [L]
First, check %{THE_REQUEST} variable. Redirect if it satisfies your match conditions.
RewriteCond %{THE_REQUEST} ^GET\ /abc\.php\?(id1)=(\d+)&(id2)=(\d+) [NC]
RewriteRule ^ /abc/%1/%2/%3/%4? [R=301,L]
Next, internally rewrite the friendly-url.
RewriteRule ^abc/(id1)/(\d+)/(id2)/(\d+)$ /abc.php?$1=$2&$3=$4 [NC,L]

Mod rewrite remove trailing slash but keep on directories

So far my .htaccess is like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<FilesMatch "\.(htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$">
RewriteEngine Off
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
I would like to have url with trailing slash redirected to the one without. But the slash for directories should be kept.
Any help would be very appreciated!
UPDATE:
after some more Google research i assembled some working solution for me. Both rewriting for removing trailing slash and redirect for www are working.
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^rudolfapotheke.de$ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
First, I would change your FilesMatch to a RewriteCond
RewriteCond %{REQUEST_FILENAME} !\.(?:htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and simplify the pattern for the next RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.html$ index.php [L]
or put the two rules together and test just for
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and finally the rule for removing the trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
To redirect those with trailing slash, you need to check if the path is a directory with a RewriteCond and then include a RewriteRule if the filename matchs /$
So it would look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1 [R=permanent,L]

Resources