Rewrite part of URL with htaccess - .htaccess

I'm trying to rewrite part of a URL with a 301 htaccess rewrite.
So far I got this
RewriteRule ^index.php?option=com_payplans&view=subscription&task=display&subscription_key=(.*)$ /subscriptiondetails/display/$1 [R=301,L,NC]
I want to go from
http://www.example.com/index.php?option=com_payplans&view=subscription&task=display&subscription_key=8O56WXCMQEZ5
to
http://www.example.com/subscriptiondetails/display/8O56WXCMQEZ5
Just can't quite figure out what I'm missing or doing wrong.

You need to use a RewriteCond to match query string:
RewriteCond %{QUERY_STRING} ^option=com_payplans&view=subscription&task=display&subscription_key=(.*)$
RewriteRule ^index\.php$ /subscriptiondetails/display/%1? [R=301,L,NC]

Related

.htaccess help for redirect

Need some help trying to redirect a domain:
https://www.example.com/blog/page/10/?page_id=%2Ffeed%2Fatom%2F to /blog/
I think the best way is to just redirect anything after /page
The code below is what I'm thinking but I know is not correct:
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/[0-255]* [NC]
RewriteRule ^ /blog/? [NC,R=301,L]
Any advice?
Need some help trying to redirect a domain:
If you are trying to redirect the domain use something like
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteRule ^(.*)$ "http://www\.exampe2\.com\/blog/" [R=301,L]
If you want to append the request to the URL use $1
If you're rewriting the path to /blog/ and want to keep the query string use the [QSA] flag ie.
RewriteRule /blog/(.*) /blog/ [R=301,QSA,L]
this will make sure you keep your query string.

How to remove part of the URL and redirect using mod_rewrite in htaccess?

I would like to remove part of the url of my Joomla site and redirect to another url for example
www.example.com/xxx/yyy/zzz/FAQ
www.example.com/123/456/FAQ
to
www.example.com/FAQ
I'm very new to mod_rewrite in .htacesss
what RewriteRule can I write to achive this
thank you
Try this in root/htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} /xx/yy/zz/([^\s]+) [NC]
RewriteRule ^ /%1? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /xx/yy/zz/$1 [NC,L]
And the same rule can be used to rewrite /123/456/faq to /123/456/FAq , just change the rewrite target and the pattern in first Rewritecond.
$1 is part of the regex in rewrite rule, its the part matched between (.+) ,it contains uri.
Thank you, after I tried I finally got it working
this is my solution
RewriteEngine On
RewriteRule ^.*/FAQ$ /FAQ [L,R=301]
by doing this I remove everything between the URL and FAQ
www.abc.com/SOMETHING/SOMETHING2/FAQ
and redirect the URL to
www.abc.com/FAQ
I also found a nice tool to test the mod_rewrite code
http://htaccess.madewithlove.be/

Remove file name in a URL

How can i remove a file name in url with mod_rewrite.
ex: searchpage-some+search.html in to some+search.html
This is my .htaccess code code.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /searchpage-search-%2.html? [R,L]
#Internal rewrite
RewriteRule searchpage-search-(.*)\.html$ searchpage.php?search=$1 [L]
This is for a search form that uses $_GET request. This works well only thing is that i want to remove the file name. I would really appreciated if anyone can help out.
In your example you have searchpage-some+search.html but in your htaccess you are actually rewriting it to searchpage-search-some+search.html.
But to remove the searchpage-search from links, just remove searchpage-search- from the RewriteRule. Your htaccess would be something like
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /searchpage\.php\?search=(.*)\ HTTP
RewriteRule ^searchpage\.php$ /%2.html? [R,L]
#Internal rewrite
RewriteRule (.*)\.html$ searchpage.php?search=$1 [L]

htaccess rewrite ?p= (I don't understand the code)

I think I have read everything I can about htaccess rewrites and I still can't make heads or tails of whats going on. I re made a website for work and all is well except the last designer did some crazy php stuff and all the urls he used have ?=p(pagename) I want to rewite those to (pagename).php then redirect them to with a 301 I am able to get the 301 redirects works just can't figure out how to rewrite the ?p=(pagename) to (pagename).php
You want to be matching against the actual requests, then internally rewrite it back to the query string:
RewriteEngine On
# 301 redirect to php file
RewriteCond %{THE_REQUEST} \ /\?p=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1.php?%2 [L,R=301]
# internally rewrite to the query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /?p=$1 [L,QSA]
You need to check the QUERY_STRING and then apply the rewrite rule
RewriteCond %{QUERY_STRING} ^p=(.*)$
RewriteRule ^(.*)$ http://mydomain.com/%1.php [R=301,L]
the ^p=(.*)$ checks for a query string that only has the one variable p=pagename, you will have to modify it if there will be any other variables in the query string it like p=pagename&id=15 etc

How to rewrite /?custom_ref= with /?ref= using htaccess rewrite

How to rewrite
/?custom_ref=
with
/?ref=
using htaccess rewrite???
RewriteEngine On
RewriteCond %{QUERY_STRING} custom_ref=(.*)
RewriteRule (.*) /?ref=%1 [R=301,L]
Can you please tell me the directory/file that you using this query on so I make sure this code will only work with it.

Resources