.htaccess rewrite portion of URL - .htaccess

I have two requirements:
I want to rewrite component/testcomp in URL to just comp.
https://www.website.com/component/testcomp should look as:
https://www.website.com/comp in URL.
All requests coming fo website.com should be 301 redirected to https://www.website.com
Below is the content of my .htaccess file:
IndexIgnore *
Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteBase /
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L]
How can I do so? I tried adding Rewrite Rule but it is not working. Let me know the Rewrite rule & where should I place it.

To rewrite "/component/testcomp" to just "comp", I think you have to flag it with R, or use R=301 to make it a permanent. I have a feeling you may have to adjust additional rules, however.
RewriteRule ^component/testcomp/(.*)$ /comp/$1 [L,R]
or to make it permanent:
RewriteRule ^component/testcomp/(.*)$ /comp/$1 [L,R=301]

Try the following:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /component/test(comp)(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,QSA]
and then,
RewriteRule ^comp(.*)$ /component/testcomp$1 [QSA,NC,L]

Related

.htaccess redirect folder only and not redirect the same folder with variables

I'm trying to redirect this
www.mydomain.com/womens/badfolder -> www.mydomain.com/womens/goodfolder
but I do not want to redirect the variables that "badfolder" has.
I would like
www.mydomain.com/womens/badfolder?page=variable
to stay as is. Is this possible?
edit
Here are my rules
RewriteEngine On
RewriteCond %{QUERY_STRING} page=shop.recommend [NC]
RewriteRule . - [F]
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and below that I'll write my redirects.
You can use this rule as your very first rule in your .htaccess just below RewriteEngine On line:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(womens)/badfolder$ /$1/goodfolder [L,NC,R]

Rewriting URL that contains multiple parameters with multi-word strings (with mod_rewrite)

Was trying to rewrite the URL of a page that has multiple parameters and one of the parameters contains multiple words (separated by spaces when submitted).
This would be the URL: www.ABCD.com/store/itemsDescr.php?categ=headbands&id=123&name=brown%20with%20black
I would like the URL to show like this:
www.ABCD.com/store/headbands/123/brown-with-black
I tried this, but it did not work:
RewriteCond %{QUERY_STRING} ^categ=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &categ=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php$ $0/%1
RewriteCond %{QUERY_STRING} ^id=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &id=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/[^/]+$ $0/%1
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &name=([^&]+) [NC]
RewriteRule ^store/itemsDescr\.php/([^/]+/[^/]+)$ http://www.ABCD.com/store/$1/%1/? [L,QSA,NC]
Any help would be much appreciated!
NOTE: Before this rule I want to implement, I already have this other rule for another page, in case it can create conflict:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(store)/index\.php\?categ=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^store/(.+?)/?$ /store/index.php?categ=$1 [L,QSA,NC]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^categ=([^&]+)&id=([^&]+)&name=([^&]+)$ [NC]
RewriteRule ^(store)/itemsDescr\.php$ /$1/%1/%2/%3? [R=302,L,NC,NE]
RewriteRule ^(store)/([^\s]+)\s([^\s]+)$ /$1/$2-$3 [R=302,L,NC]
RewriteRule ^(store)/([^\s]+)\s(.+)$ /$1/$2-$3 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(store)/([^/]+)/([^/]+)/ /$1/itemsDescr.php?categ=$2&id=$3 [L,QSA,NC,NE]

Joomla .htaccess

I need a working RewriteRule to get rid of the /index.php/category/ part of my url(s).
bruteforce.tv/index.php/streams/shinigamily
bruteforce.tv/index.php/streams/cash
bruteforce.tv/index.php/streams/blacktigrex
bruteforce.tv/index.php/streams/viktorwwe
TO
bruteforce.tv/shinigamily
bruteforce.tv/cash
bruteforce.tv/blacktigrex
bruteforce.tv/viktorwwe
I'm not used to it with working with a htaccess file so I apologize for being completely unresourceful.
This is my current htaccess file with the not working RewriteRule ^streams/(.*)/?$ /$1 [L,NC,R]
Options +FollowSymLinks
## Mod_rewrite in use.
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^streams/(.*)/?$ /$1 [L,NC,R]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Use Joomla's built-in feature to remove /index.php (under global config > site)
RewriteRule ^streams/(.*)$ http://bruteforce.tv/$1 [R=301,L]

joomla htaccess RewriteRule issue

i have a redirect in my htaccess which works perfectly in joomla before i swicth on SEF URLS
RewriteRule ^m/(.*) /index.php?option=com_seemail&view=mail&guid=$1&itemid=102
however when i switch on SEF URLS then it no longer works - im directed to /home/uncategorised/welcome instead
this is my htaccess :
RewriteEngine On
RewriteRule ^m/(.*) /index.php?option=com_seemail&view=mail&guid=$1&itemid=102
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^apply$ /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
RewriteBase /
DirectoryIndex index.php
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule ^(.*)$ index.php [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
can anyone help please ?
RewriteRule ^apply$ /index.php?option=com_loans&view=apply&Itemid=102 [R=301,L,QSA]
based on the above line of code, if there is nothing after "apply" in the url, it should redirect to /index.php?option=com_loans&view=apply&Itemid=102 and stop looking at other rules for the said case.
As you said it is redirecting that means it is working.
Can you manually open the <yourdomain.com>/index.php?option=com_loans&view=apply&Itemid=102
If it opens and the url is sef, copy the sef url after the domain and replace /index.php?option=com_loans&view=apply&Itemid=102 with that in the RewriteRule.
May be that should work.

Delete ugly ?pg=1 from URL via .htacess RewriteRule

I have pagination plugin in my CMS which produces ?pg=1 links. I want to redirect this url without this ugly postfix because without it CMS shows first page too.
So, i have url like http://site.ru/category/schock-crane/?pg=1 or http://site.ru/moyka/?type=granit&pg=1
I want to redirect such urls to http://site.ru/category/schock-crane/ and http://site.ru/moyka/?type=granit respectly.
I tried this .htaccess code
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
I tried this regexp code at regexp trainers - it worked. But at live server no redirect happens.
Here is whole .htaccess file:
AddDefaultCharset Off
#DirectoryIndex index.php index.html
Options +FollowSymLinks
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# fix /?pg=1
RewriteRule (.*)(&|\?)pg=1$ $1 [R=301,L]
# fix .palitra-tab
RewriteCond %{REQUEST_URI} -tab$
RewriteRule (.*)/([0-9]*)/\.(.*)-tab?$ http://site.ru/redirect.php?id=$2 [R=301,L]
RewriteCond %{REQUEST_URI} ^/csss/.* [NC]
RewriteRule csss/(.*)\.css$ css.php?n=$1 [L]
#RewriteRule ^(.*)/csss/(.*) /test.php [L]
RewriteRule ^(.*)/base([0-9]+)/?$ $1?base=$2
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule \.(gif|jpeg|jpg|png)$ /images/watermark/index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
RewriteCond %{HTTP:Authorization} !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>
You need to use RewriteCond to examine the query string:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)pg=1(&(.*)|$)
RewriteCond %1%4 (.*?)&?$
RewriteRule ^ %{REQUEST_URI}?%1 [R=301,L]
The second RewriteCond is just to remove a trailing &.
Maybe the missing ^ or / kills the redirect
# fix /?pg=1
RewriteRule ^(.*)(&|\?)pg=1$ /$1 [R=301,L]
or try
RewriteRule ^(.*)(&|\?)pg=1$ http://%{HTTP_HOST}/$1 [R=301,L]

Resources