301 redirect rule in htaccess - .htaccess

What am i doing wrong? this rule isn't working
I want the xyz-banking(old) to xyzbanks(new)
RewriteRule ^category/xyz-banking/?$ http://www.domainname.com/category/xyzbanks/ [L,R=301]
When there are multiple categories goes to one new category...can i do this?
RewriteRule ^category/(chicos|chs|rl)/?$ http://www.domain.com/category/apparel/ [L,R=301]
Ofcourse that one is also not working.

Both rewrite rules seem to be correct and they should work.
Whenever you browse to: http://www.domain.com/category/chs/ you will be redirected to http://www.domain.com/category/apparel/ as requested, etc.
Maybe you have a problem somewhere else:
Is mod_rewrite enabled?
Make sure you have RewriteEngine On on your .htaccess or site configuration.
If using an .htaccess file, Make sure you have an AllowOverride directive that allows you to use RewriteRule there.
Try a simple redirect rule first!
Check your error and access logs to see if you can spot any error or warning message.
Take a look at the URL Rewriting Guide - Apache HTTP Server

Related

Rewrite a complex link .htaccess

My old website have product link like this
domain-name/index.php/product-category/detail/603/product-name.html
my new Wordpress website have product link like this
domain-name/product-category/product-name
i config in .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^product-category/detail/([0-9]+)/([a-zA-Z-]*).html /product-category/$2 [QSA,L]
in permalink i config:
/%category%/%postname%
The new site works very correctly, but the old link is not working. Old link always redirect to error 404 absolute, i want to keep old link on google auto redirect to new link. Does anyone have a solution for this please, thank u very much.
This should be your rule modified to actually match the requested URL you show:
RewriteEngine On
RewriteRule ^/?index\.php/product-category/detail/\d+/([\w-]+)\.html /product-category/$2 [QSA,END]
But more likely you want the more flexible variant:
RewriteEngine On
RewriteRule ^/?index\.php/([\w-]+)/detail/\d+/([\w-]+)\.html /$1/$2 [QSA,END]
You should prefer to implement such rules in the actual http server's host configuration. If you have no access to that you can use a distributed configuration file (often called ".htaccess"), but that comes with some disadvantages.
I could imagine that it also makes sense to send an external redirection to clients still requesting those old URLs:
RewriteEngine On
RewriteRule ^/?index\.php/([\w-]+)/detail/\d+/([\w-]+)\.html /$1/$2 [QSA,R=301,END]
For that it might make sense to start out with a R=302 temporary redirection and to change that to a R=301 permanent redirection once you checked that everything works as expected. That prevents annoying caching issues...

URL rewriting .htaccess help needed

Assume I have two domain names example1.com and example2.com. I forwarded example1.com to example2.com using the domain forwarding on hosting panel. It works fine but when I go to example1.com/sub it just shows example2.com as the URL. I want it to show example2.com/sub. I tried URL rewriting but no luck so far. It just keep loading and shows nothing. Am I missing something ?
This the rule I used.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example1.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301]
First make sure that you've cleared your browser's cache. If it's a 301 redirect your browser will cache the redirect. Then make sure to turn off your hosting panel's forwarding.
Other than that, your rule should work fine, assuming it's at the top of the htaccess file in example1.com's document root.
In RewriteCond rule you use !, which mean negate the result of the condition.

301 Redirect .mp4 file to a link

I have a link to a video file (example.com/abc.mp4), which, when clicked, I would like to redirect to another link. We don't want to change the link on the page since this link was given out, so it would make more sense to redirect it, if possible.
I would think there should be a way to do this via .htaccess and RegEx, but have not been able to have any luck finding a solution, or talk about something like this.
Does anyone have any ideas?
There are two chances: either you're doing the redirection rule wrong, or your Apache is ignoring the .htaccess directives. ¿Do you know if another rewrite rules are working? ¿Can you check if your vhost if configured with AllowOverride All?
Additionally, you could try the following rule to redirect a file which doesn't exist. Just to be sure:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^xyz\.mp4 http://www.newsite.com/newfile.mp4 [r=301,L]
</IfModule>

URL rewrite works with 301 or absolute path but not without

I'm trying to show the user the content of http://www.mysite.com/booking-request?code=### when visiting a url such as http://www.mysite.com/booking/ZHTU78
I'm sure this is simple, but I cannot understand why the following rule is not working for me. All I get is a 404 error.
RewriteRule ^booking\/([\w\d]+)$ /booking-request?code=$1 [L]
If I change the rule to
RewriteRule ^booking\/([\w\d]+)$ /booking-request?code=$1 [L,R=301]
It works, but with the obvious undesirable side effect of actually redirecting the browser to http://www.mysite.com/booking-request?code=###
I've also tried changing the url to redirect to to a single / to make sure it should be looking for an existing page, but no change.
It might be pertinent to mention this is on a drupal site and /booking-request is essentially a basic page, so drupal will have some functionality to redirect this page anyway, however I wouldn't have thought it should matter in this case.
edit
Here's a link to the whole htaccess file: http://pastebin.com/KPE6DK6N
Try change the rule to:
RewriteRule ^booking/([\w\d]+)$ /booking-request?code=$1 [L]
or
RewriteRule ^booking/([\w\d]+)$ http://www.mysite.com/booking-request?code=$1 [L]
It should work.

PHP and .htaccess redirect woodoo

I'm a little stuck with with my .htaccess redirect.
It was working find while I was with PHP4 but the recent move to a new host with PHP5 have changed things for which I've no clue.
I'm working on a URL shortening service. Here, for a URL like http://example.com/e72b0f, it gives me http://example.com/forward.php?e72b0f
Earlier with my .htaccess file, the "forward.php?" was masked (hidden). How can I bring back this behavior. Here is the .htaccess for your reference.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \/([0-9a-z]{6})$ [NC]
RewriteRule ^(.*) http://example.com/forward.php?%1 [L]
Btw, I also do not rule out the issue being in the PHP Script. The developer that did it for me is too busy to look at it.
If you rewrite to an http:// URL that the server doesn't think is in the site, mod_rewrite will do a redirect instead of just a rewrite. In order to see if this is happening, make a page that has nothing in it but
<?php echo $_SERVER['SERVER_NAME']; ?>
and see if says it's going to "nsfw.in".
Either way, you should be able to strip off the http:// nsfw.in from the beginning of the URL and just rewrite it to /forward.php?%1. You may need to add a PT flag in order for it to be interpreted as a URL and not a FS path.

Resources