URL Masking with htaccess - .htaccess

I have a url below:
http://www.mysite.com/mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price=
if PropertyBuyRent=rent and all other variables are empty then url should be like:
http://www.mysite.com/mypage.php/TimeshareForRent/All
how can i write htaccess for this?

You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(testing/mypage\.php)\?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [NC]
RewriteRule ^ /%1/TimeshareForRent/All? [R=301,L]
RewriteRule ^(testing/mypage.php)/TimeshareForRent/All/?$ /$1?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [L,NC,QSA]
Reference: Apache mod_rewrite Introduction

You can use this line and make edits in php file as follows
RewriteRule ^mypage.php/TimeshareForRent/All/?$ mypage.php?PropertyBuyRent=rent&Developer=&Resort=&City=&State=&Country=&Price= [L,NC,QSA]
You can tell your php file to redirect to "mypage.php/TimeshareForRent/All" when your condition matched, because with above rule, it will mask the url of variables with the one without variable

Related

how to redirect through htaccess

How to create redirection rule in .htaccess for:-
earlier we have 400+ paths like.
/blog/some-path1
/blog/some-path2
but now we have paths like.
/some-path1
/some-path2
How to create single line redirection rule for above redirection without write multiple redirections.
tried below rule but not working
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(.*)$ /$1 [L,NC,R]
Try with below rule, I am assuming you know the paths are defined correctly, clear cache beforehand.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^blog/(.+)
RewriteRule ^ /%1 [R=301,L]

htaccess - remove segment of url with redirect

Need to use .htaccess to redirect all urls that have blog-entry in path like this:
http://example.com/blog/blog-entry/blog-title
to this:
http://example.com/blog/blog-title
Tried this per another stack answer, but no luck:
RewriteRule ^blog-entry/(.*)$ $1 [L,R=301,QSA]
Please advise.
Change your rule with this rule:
RewriteCond %{THE_REQUEST} \s/+(.*)/blog-entry/(\S*) [NC]
RewriteRule ^ /%1/%2 [R=301,NE,L]
Since we're using THE_REQUEST here, this rule will work from site root or /blog/ directory.

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]

Rewrite rule .htaccess from blog.domain.com/anypath to www.domain.com/blog/anypath

I'm trying to write a rule in my .htaccess file so that any visitors from blog.domain.com/anypath are redirected to www.domain.com/blog/anypath
The rule that I've written below only seem to redirect blog.domain.com to domain.com/blog but doesn't seem to redirect correctly if the URL contains a path such as blog.domain.com/path
RewriteCond %{HTTP_HOST} ^blog\.* [NC]
RewriteRule .* http://www.domain.com/blog [L]
Any help is appreciated.
This is because you have not captured the path with a () capture group to rewrite via the variable $1. You will need to append that to your redirection URL.
RewriteCond %{HTTP_HOST} ^blog\.* [NC]
# Capture the full path into $1 and append it to the output URL
RewriteRule (.*) http://www.domain.com/blog/$1 [L,R=301]
If this is to be a permanent redirection, you should use R=301 in [L,R=301]
As always, consult the mod_rewrite documentation for full details, and use this clever rewrite tester for experimentation.
You'll need to capture your REQUEST_URI and pass it onto the path.
RewriteCond %{HTTP_HOST} ^blog.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
This will capture the path from the blog's subdomain, and redirect it to the correct URL.
For example, blog.domain.com/post/736/test will redirect to www.domain.com/blog/post/736/test.

Redirect existing file to different url using mod_rewrite

I'm trying to use mod_rewrite to redirect an existing file to a different URL. I'm using the following and it has no effect. I've tried several variations of which don't work.
RewriteEngine on
AddHandler x-httpd-php .php3
# AddHandler x-httpd-php5 .php .php4
# This file exists, but this redirect doesn't work
RewriteRule ^show.php?id=review-1$ /review/1/super-baseball-2020/ [R=301,L]
Does it by chance have something to do with the url params?
You cannot have query string in RewriteRule. RewriteRule matches only URL part that's why your new rule is not working. You need to have a separate RewriteCond to match QUERY string. It should be re-written like this:
# for external redirect from /shows.php?id=review-1 to /review/1/super-baseball-2020/
RewriteCond %{THE_REQUEST} ^GET\s/shows\.php\?
RewriteCond %{QUERY_STRING} (?:^|&)id=([^-]*)-(.*)(?:&|$) [NC]
RewriteRule ^ /%1/%2/super-baseball-2020/? [R=301,L,NC]
# for internal redirect from /review/1/super-baseball-2020/ to /shows.php?id=review-1
RewriteRule ^([a-z0-9]+)/([0-9]+)/([a-z0-9-]+)?/?$ shows.php?id=$1-$2 [L,NC,QSA]

Resources