Remove %2F from URLs with mod_rewrite - .htaccess

I would like to remove the '%252F' from my dynamically created URLs.
I have a php-file that creates links with a %2F instead of /.
The links are then represented in the URL-Bar as %252F instead of / which leads to some problems.
What I'm trying to achieve with a .htaccess-file is to redirect all %2F to / or rename all %252F to / since I can't change the php-code creating the links.
This is my .htaccess
RewriteEngine on
Options +SymlinksIfOwnerMatch
RewriteBase /
RewriteCond %{HTTP_HOST} subdomain.mydomain.com
RewriteCond %{REQUEST_URI} (.*)/style.css [OR]
RewriteCond %{REQUEST_URI} (.*)/script.js [OR]
RewriteCond %{REQUEST_URI} (.*)/logo.png
RewriteRule (.*) http://www.mydomain.com%{REQUEST_URI} [R=301,NC,L]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://subdomain.mydomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \ /([^\ \?]*)([^\ \?]*)%2f(\?.*)?\ [NC]
RewriteRule !^/ /%1/%2 [QSA]
RewriteRule ^(/.+)%2f(.*)$ $1/$2 [NC,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?dir=([^\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?dir=/$1 [L]
I can't get it to work, maybe someone else can help me with this.
Thank you very much!

The lines:
RewriteCond %{THE_REQUEST} \ /([^\ \?]*)([^\ \?]*)%2f(\?.*)?\ [NC]
RewriteRule !^/ /%1/%2 [QSA]
RewriteRule ^(/.+)%2f(.*)$ $1/$2 [NC,QSA]
Need to have the leading slash removed. URI's sent through rules in htaccess files have the leading slash stripped off:
RewriteCond %{THE_REQUEST} \ /([^\ \?]*)([^\ \?]*)%2f(\?.*)?\ [NC]
RewriteRule ^ /%1/%2 [QSA]
RewriteRule ^(.+)%2f(.*)$ $1/$2 [NC,QSA,L]
Not sure what the RewriteRule !^/ /%1/%2 [QSA] line is supposed to do, it looks like you're stripping out a trailing slash?

I managed to get rid of the first %252F in the URL and the problem of "stacking" slashes:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://subdomain.mydomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?dir=%2F([^\s]+) [NC,OR]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?dir=([^\s]+) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?dir=/$1 [L]
I still have the problem of having %252F in my URLs though.

Related

htaccess rule issue in redirecting the url

I want to redirect all below pattern url to before "?" url
i.e
Xyz.com/worksheet/rebus/?random=testing11
Xyz.com/worksheet/rebus/?abc
Xyz.com/worksheet/rebus/?l=1
should be redirected to
Xyz.com/worksheet/rebus/
I tried many things but not able to succeed. How can i do using .htaccess
Rules tried
RewriteBase /worksheet/
RewriteRule ^rebus/?$ /worksheet/rebus/ [L,R=301]
Overall
RewriteEngine On
RewriteBase /worksheet/
RewriteCond %{THE_REQUEST} \s/(rebus)/\?(\S+)\s [NC]
RewriteRule ^ /worksheet/%1/ [R=301,L]
#RewriteRule ^rebus/?$ /worksheet/rebus [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
Have it this way:
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus? [L,R=301,NC]
# skip all rules for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]

remove id and index.php via htaccess in subdirectory

current URL:
domain/index.php?id=p123
I want:
add www
remove:index.php
remove 'id' form URL
I do it this way:
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
it works if domian is without sub directory.
how can I change this code to support sub directory too like this:
domian/subdirectory/index.php?id=p123
I'll go about it this way:
First, redirect non-www URLs to www ones:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Rewrite the friendly URLs to respective index.php files:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*/)?([^/]+)$ /$1index.php?id=$2 [L]
Next, handle the raw URIs with index.php in them:
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
The last rule will also take care of URLs like domain.com/dir/?id=sth and domain.com/dir/index.php?id=sth
You can accomplish this with the following changes:
RewriteCond %{THE_REQUEST} \ /(.*/|)(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*/|)([^/]*)$ $1index.php?id=$2 [L]
We know that %{THE_REQUEST} has the format GET /path/to/index.php?id=12312412 HTTP/1.1 or something along those lines. In the first rule we match \, which is a literal space, then the beginning slash that is always there. (.*/|) will match everything until index.php, or exactly nothing. The same trick we do in the second rule. I have added RewriteCond %{REQUEST_URI} !index\.php to prevent an infinite loop when index.php does not exist for a particular directory.

Htaccess issue - url with multiple parameters

I use this htaccess for multiple parameters like bank, state,district & branch.
But it only format bank not others...???
I need it for:
www.domain.com/bank/ .....(It works)
www.domain.com/bank/state/
www.domain.com/bank/state/district/
www.domain.com/bank/state/district/branch/
My htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&\s]+)
RewriteRule ^ /%2/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank= ([^&]+)&state=([^&]+)
RewriteRule ^ /%2/%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)&state=([^&]+)&district=([^&]+)
RewriteRule ^ /%2/%3/%4/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /index\.php\?bank=([^&]+)&state=([^&]+)&district=([^&]+)&branch=([^&]+)
RewriteRule ^ /%2/%3/%4/%5/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Kindly provide solution for this...Thanks & Regards.
You need to tweak your regex and reverse order of redirect rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)&state=([^&\s]+)&district=([^&\s]+)&branch=([^&\s]+)\s
RewriteRule ^ /%2/%3/%4/%5/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)&state=([^&\s]+)&district=([^&\s]+)\s
RewriteRule ^ /%2/%3/%4/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank= ([^&\s]+)&state=([^&\s]+)\s
RewriteRule ^ /%2/%3/? [L,R=301]
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index\.php\?bank=([^&\s]+)\s
RewriteRule ^ /%2/? [L,R=301]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Make sure to clear your browser cache before testing this change.

Htaccess redirect with many rules

I got rewrite rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301] //add www before adress
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] //remove .php
RewriteRule ^ %1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d //remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php
RewriteRule ^portfolio/([0-9]+)([^.]*)$ /portfolio-single.php?id=$1 [L]
But the last one dosent work, i want to have adress like www.mysite.com/portfolio/3-some-name => /portfolio-single.php?id=3
I dont know how to get i work.
(Sorry for my bad english)
Change order of rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|static)\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^portfolio/([0-9]+) /portfolio-single.php?id=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

Removing html extentions and add trailing slash to files on all directories without affecting internal urls

I am using the code in below to redirect index.html and non-www version of URL to www. It also removes *.html extensions from the files. Now, I would like to add a trailing slash at the end of the files across all directories. Following are the examples of what I want to get:
www.mydomain.com.au/contact.html goes to www.mydomain.com.au/contact/
www.mydomain.com.au/contact goes to www.mydomain.com.au/contact/
www.mydomain.com.au/glass-replacement/Brisbane.html goes to
/glass-replacement/Brisbane/
and so forth...
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
RewriteCond %{http_host} ^mydomain.com.au$ [nc]
RewriteRule ^(.*)$ http://www.mydomain.com.au/$1 [r=301,nc,L]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Thanks for your help in advance
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^.*\/index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]
RewriteCond %{http_host} ^glassnow.com.au$ [nc]
RewriteRule ^(.*)$ http://www.glassnow.com.au/$1 [r=301,nc,L]
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.html)?(\?.*)?\ [NC]
RewriteRule ^(.+/)?index(\.html)?$ /%1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.glassnow.com.au/$1/ [R=301,L]

Resources