Redirect to a Rewritten URL - php

OK, so I'm rewriting some page URLs for a custom PHP cart.
I've got the following rules:
RewriteRule ^category/([0-9]+)/([a-z-]+)$ /store.php?cat=$1 [L]
RewriteRule ^product/([0-9]+)/([a-z-]+)$ /product.php?id=$1 [L]
These will allow me to use a url structure like example.com/product/23/product-slug.
That part is working alright. I'm wondering what options I have for the other direction; redirecting requests for the OLD url to the NEW url. So for example, when someone goes to /product.php?id=2 I want to redirect to /products/2/slug.
Any idea how to get this done?
I tried a simple redirect, but would not work:
Redirect 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug

Redirect only takes a url prefix, not a regex (e.g. /store.php or /store)
You need to try RedirectMatch:
RedirectMatch 301 ^/store\.php\?cat=16$ http://www.example.com/category/16/category-slug
Also, is it supposed to start with a /? I'm not sure (your RewriteRule entries above start with no slash, for example)

I solved this a different way: with a modification to the store.php file.
I looked at output from print_r($_SERVER) after pinging both the normal and rewritten urls. I found that $_SERVER['SCRIPT_URL'] contains "/store.php" when the normal url is hit and it contains my rewritten path when the rewritten url is hit.
This means I can do a simple test and redirect appropriately:
if ($_SERVER['SCRIPT_URL'] == "/store.php") {
// run some code that will generate the url
$rewrittenURL = generateURL();
// then add:
header("HTTP/1.0 301 Moved Permanently"); // tell spiders this is permanent
header("Location: $rewrittenURL");
}

Related

htaccess redirect not working in same domain

I want to redirect https://xxx.xxxx.com/zzz to https://xxx.xxxx.com/zzz247. I have tried this
Redirect 301 / https://xxx.xxxx.com/zzz247
So now when I try https://xxx.xxxx.com/zzz its take me into https://xxx.xxxx.com/zzz247zzz247zzz247zzz247zzz247zzz247zzz247....
How can I fix this issue? help please
What is required
I need enable only one URL which is https://xxx.xxxx.com/zzz247 even user type https://xxx.xxxx.com/zzz
Instead of Redirect directive use RedirectMatch with regex support:
RedirectMatch 301 ^/zzz/?$ /zzz247
Redirect appends current URI into redirected URI and since you're matching / that will match anything and cause a redirect loop.
Make sure to clear your browser cache before testing.
Using RewriteRule you can do this:
RewriteRule ^zzz/?$ /zzz247? [L,NC,R=301]
? at the end of the target URI is for stripping any query string.

Magento htaccess redirection issue

I was trying to redirect a particular URL and all its child URL's to a particular URL. Using 301 redirection this is working fine but the browser URL is showing the test contents also. Please see below for more details
Example: https://www.mywebsite.com/customer/account/login/referer/* needs to be redirected towards https://www.mywebsite.com/customer/account/login/register
What I tried is:
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /customer/account/login/referer https://mywebsite.com/customer/account/login/register
The htaccess rule is working fine for just this URL. If I type
https://mywebsite.com/customer/account/login/referer/testing
This will be redirected towards https://mywebsite.com/customer/account/login/register/test
Please let me know how I will be able to trim the other part (URL part after register/*)from the redirected URL. What I am trying to achieve is
https://mywebsite.com/customer/account/login/referer/* [ANY URL's AFTER referer/ (Including /referer) needs to be redirected to https://mywebsite.com/customer/account/login/register].
This is expected behaviour, as stated in the documentation of Redirect
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
To redirect all requests starting with this prefix to the given URL, you may use RedirectMatch, which uses regular expressions instead of a URL prefix
RedirectMatch ^/customer/account/login/referer https://mywebsite.com/customer/account/login/register
You don't need to use
Options +FollowSymLinks
RewriteEngine on
because this is for mod_rewrite. Redirect and RedirectMatch are provided by the mod_alias module.

301 Redirect: index.php

I am familiar with redirecting URLs that don't have file types at the end, but not with ones that have .php at the end.
The website I'm working on has hundreds of indexed pages on Google that have this at the beginning of the url: http://example.com/index.php? with more information trailing afterwards.
An example is: http://example.com/index.php?main_page=index&cPath=1_18
The new index of the website is http://example.com/index or http://example.com
If I put http://example.com/index.php as the URL to be redirected, will it also redirect all index.php? Or do I need to put http://example.com/index.php* as the URL to be redirected or http://example.com/index.php?*?
Thank you!
If the main script to be executed is index.php, then there is no point in redirecting a url to itself...
Aabout those GET parameters in the url, if index.php oesn't use them, then.... they will be useless, but... thats it XD
The point is: does everything work without redirecting? If so, why redirecting at all?
If there are 301 redirects, ... that will mean more request to the server, and for those clients with low bandwith it does make a difference.
BTW, take a look at: htaccessredirect
Try:
RedirectMatch 301 ^/index\.php$ /
(or)
RedirectMatch 301 ^/index\.php$ /index
The query string at the end will automatically get appended.
If you're using wordpress now, you're not going to be able to use the mod_alias directive like above. You'll need to use mod_rewrite and place these rules above the wordpress rules:
RewriteCond %{THE_REQUEST} \ /+index\.php
RewriteRule ^ / [L,R=301]
(or replace the / at the end with /index)

htaccess redirect url with var from no file extension to file extension

the problem is that for some reason googlebot gets a bunch of error 404s when crawling a certain page but everything works fine on my end, I think...
using htaccess, I have rewritten a page with vars for seo purposes, so:
RewriteRule ^feeds/([^/]*)\.xml/?$ /rss/feeds.php?cat=$1 [QSA,NC,L]
so I get www.url.com/feeds/category.xml
instead of
www.url.com/feeds.php?cat=category
I've also ajusted all urls on the page to follow this rule and like I said everything works fine on my end... googlebot for some reason only sees www.url.com/feeds/category and does see the .xml extention.
not sure if it would work but I was hoping to put a rule in htaccess to redirect www.url.com/feeds/category to www.url.com/feeds/category.xml in the even that this happened, but was unsure of how to do so.
Maybe try
RewriteRule ^feeds/([^/]+)/?$ /feeds/$1.xml [R,NC]
Which would redirect www.url.com/feeds/category to www.url.com/feeds/category.xml, using a HTTP 302 Redirect. Remove the R flag if you don't want it do use the HTTP 302 Redirect, but that would make www.url.com/feeds/category a valid URL as far as google is concerned. Just in case: mod_rewrite.

Redirect 301 / Rewrite conundrum

On my site, users can add various URL's that need to be redirected.
For example; from this: domain.com/oldpage/36/
To this: domain.com/newpage/47/
They are added to the .htaccess like this:
Redirect 301 /oldpage/36/ /new-page/47/
But when accessing the old page they get this:
domain.com/newpage/47/?pid=36&pagename=oldpage
I'm pretty sure these rewrite rules are causing this predicament:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3
However, mod_rewrite stuff is not my strongpoint, so I have no idea how to fix it.
Any ideas ?
Adding a ? makes the Rewrite not add the query string to the url.
so this should work:
Redirect 301 /oldpage/36/ /new-page/47/?
As a precaution you could also add it to the end of:
RewriteRule ([^.]+)/([0-9]+)/$ index.php?pid=$2&pagename=$1?
RewriteRule ([^.]+)/([0-9]+)/([^.]+) index.php?pid=$2&pagename=$1&vars=$3?
But only if they are needed
Since you are already using mod_rewrite anyway, I suppose you should make your redirects using rewrites too
RewriteRule /oldpage/36/ /new-page/47/ [R=301]
This will "rewrite" the URL from old to new, and will redirect the browser to new url with status code 301. [R] directive means redirect, which also stops other rules from processing, hence the other rules will be handled only when the new request is sent from broswer with new url.

Categories