.htaccess - rewriting and then hiding part of the url? - .htaccess

I have the following RewriteRule in my .htaccess:
RewriteRule ^products/([A-Za-z0-9-\s\#]+)/([A-Za-z0-9-\s\#]+)/?$ /store/products/product.php?prod=$1&src=$2 [L,QSA]
It takes a url such as:
http://example.com/store/products/lawnmower/blogThatLovesUs
and shows the page here:
http://example.com/store/products/product.php?prod=lawnmower&src=blogThatLovesUs
Is there a way I can edit this RewriteRule so that the user just sees
http://example.com/store/products/lawnmower/(sans blogThatLovesUs)
in their address bar?

Sure, you can do that, but you will lose your affiliate's ID (blogThatLovesUs) in the process. You could use your original URL to redirect as you are doing, and then have the resulting page (product.php) cache the affiliate value in session and then do a standard redirect in PHP (with header()) to the updated URL without the affiliate value. Does that make sense?
I want to illustrate to be more clear:
URL: http://photojojo.com/store/products/lawnmower/blogThatLovesUs
through RewriteRule you end up with:
URL: http://photojojo.com/store/products/product.php?prod=lawnmower&src=blogThatLovesUs
At this point, in product.php you store blogThatLovesUs off in session somewhere and then:
<?php
header("Location: http://photojojo.com/store/products/lawnmower/");
?>
To get the browser to the URL you want them at.

Related

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

htaccess Rewrite rule to send variable info to script but display clean url

What I'd like to do is when a URL like
http://localhost/sandbox/jcsearch/country/countryname
is typed by the user I would like to to point to
http://localhost/sandbox/jcsearch/index.php?country=countryname
but still retain the original clean URL in the address bar ie
http://localhost/sandbox/jcsearch/country/countryname
Is this possible? would it create any kind of redirect loop?
The rewrite would happen as follows:
RewriteEngine on
RewriteRule ^sandbox/jcsearch/([^/]+)/([^/]+)$ sandbox/jcsearch/index.php?$1=$2 [L,NC]
Since, the RewriteRule directive does not have the redirection flag (R) set, it will not change the URL in your browser's addressbar. So, by visiting
http://localhost/sandbox/jcsearch/country/countryname
user will get internally redirected to:
http://localhost/sandbox/jcsearch/index.php?country=countryname
Please note: You have to put the rewrite rules in the htaccess file in your server root directory.

forcibly redirect to correct folder

I am new in htaccess.
I updated some SEO pages in my live site after one day some Url changes came so i changed the url again. but google already indexed it. So i want if some one found old url it will redirect to new url But in case of SEO pages only not for other pages.It means it wont affect to any other place.and there are not one page(it is 40-50 pages) can anybody give answer through htaccess or cakephp.
Old Url-
www.testenergy.com/test-energy-reviews
new url-
www.testenergy.com/s/test-energy-reviews
And there are also four senario-
www.testenergy.com/test-energy-reviews
www.testenergy.com/Test-Energy-Reviews
www.testenergy.com/s/test-energy-reviews
www.testenergy.com/s/Test-Energy-Reviews
All these four links will redirect to www.testenergy.com/s/test-energy-reviews Url only
Assuming you have mod_rewrite rules somewhere, you probably want to stick to mod_rewrite. You'll need to add these to the htaccess file in your document root, preferably above any other rules that are there:
RewriteEngine On
RewriteRule ^/?test-energy-reviews$ /s/test-energy-reviews [L,NC,R=301]
RewriteRule ^/?s/Test-Energy-Reviews$ /s/test-energy-reviews [L,R=301]
The NC flag ignores case, so it covers both /test-energy-reviews and /Test-Energy-Reviews. The second rule takes care of /s/Test-Energy-Reviews
I'm not sure why /s/test-energy-reviews (3rd one) is one of your scenarios, since it is exactly what you want to redirect to.
Try This ..!!
Router::redirect('/test-energy-reviews', 'http://www.testenergy.energy/s/test-energy-reviews');
write this line in Controller.
/********* Redirect Url fo small letter if some one type in uppercase in url bar****/
preg_match( '/[A-Z]+/',$this->params->url, $upper_case_found );
if(count($upper_case_found)) {
// Now redirect to lower case version of url
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . ROOTPATH.strtolower($this->params->url) );die();
}
/**** End Code******/
OR in htaccess write following code:
RewriteEngine On
RewriteRule ^/?test-energy-reviews$ /s/test-energy-reviews [L,NC,R=301]

Is possible redirecting without change the url in .htaccess?

I have this url:
http://localhost/search/
This returns me this file:
http://localhost/search.html
Now I want the urls with this structure:
http://localhost/search/([^/]+)/([^/]+)/([^/]+)/?
will redirect me to the search.html file too. But without changing the url.
For example with this urls:
http://localhost/search/women/23/shoes/
http://localhost/search/
http://localhost/search/man/45/shirt/
would return the same file:
http://localhost/search.html
Note: the urls of man and women does not has any existing path in the server.
Any advice or help would be appreciated. If you need more info, let me know and I'll edit the post.
RewriteEngine on
RewriteRule ^search/ /search.html
Will just work fine. Unless you explicitly request an external redirect, a RewriteRule on the same domain will not do one, thus not changing the URL visible in the browser.
if you don't need the rest of url then you can use this
RewriteEngine On
RewriteRule ^search/(.*)$ /search.html [L]
if you need to other parameters of url then let me know
edited version, Niels Keurentjes has a point if you don't need the rest of url
RewriteEngine On
RewriteRule ^search /search.html

Using .htaccess to change what URL is being displayed, but without actually redirecting the content?

I have a RewriteRule setup to change
http://kn3rdmeister.com/blog/post.php?y=2012&m=07&d=04&id=4
into
http://kn3rdmeister.com/blog/2012/07/04/4.php
but that actually redirects where the browser is getting the page from. I want to still display
/blog/post.php?y=xxxx&m=xx&d=xx&id=xx
but have the browser show the simpler URL like
/blog/post/year/month/day/id.php
I read something somewhere about using ProxyPass, but I don't quite know what I'm doing :P
I want people to be able to visit either the post.php URL with the query strings, OR the clean URL with fancy shmancy subdirectories for the dates and get the same content — all while displaying the clean URL in the end.
You want something like this:
# First if someone actually requests a /blog/post.php URL, redirect them
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD)\ /blog/post\.php\?y=([0-9]{4})&m=([0-9]{2})&d=([0-9]{2})&id=([0-9]*)\ HTTP/
RewriteRule ^blog/post\.php$ /blog/%2/%3/%4/%5.php [R=301,L]
This will redirect the browser to the /blog/##/##/##/##.php URI, that will show up in their address bar. Once they get redirected, the browser will then send a request for /blog/##/##/##/##.php and your server then needs to internally rewrite it back:
# We got pretty URLs, but need to rewrite back to the php request
RewriteRule ^blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^\.]+)\.php$ /blog/post.php?y=$1&m=$2&d=$3&id=$4 [L]
This changes everything back internally so that the file /blog/post.php can handle the request.

Resources