Rewrite a complex link .htaccess - .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...

Related

Issue using .htaccess on a linux server

So I have a site where once there was a wiki structure in php
Now I have a completely clean site now started up from scratch (so no hidden .htaccess files etc. or other installed components) but need to redirect incoming url requests for the old site to a new html structure
I imagined I could do it using an .htaccess file
I have tried a variation of syntax in the .htaccess file for example:
<IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectPermanent 3dprintfo.com/doku.php?id=3dproject2 http://3dprintfo.com/3dproject2.html
<IfModule mod_rewrite.c/>
<IfModule>
And for example:
RedirectPermanent 301 3dprintfo.com/doku.php?id=3dproject2 http://3dprintfo.com/3dproject2.html
RedirectPermanent http://3dprintfo.com/doku.php?id=3dproject2 http://3dprintfo.com/3dproject2.html
However nothing works I keep getting:
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I have tried with and without http/301 etc.
Can anyone try to show me an example with multiple url redirects that I can try to implement using my above url names...
This would be an example of a working redirection, as far as I understand your question:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?3dprintfo\.com$
RewriteCond %{QUERY_STRING} ^id=3dproject2$
RewriteRule ^/?doku\.php$ /3dproject2.html [R=301,QSD,L]
Of course it is possible to implement more general rules, so that you won't need a catalog of rules for each and every "old page". But that obviously depends on your link structure used previously.
In general I would suggest that you should implement such rule in the actual http server's host configuration and not in a distributed configuration file (".htaccess"). But above rule is written such that it works in both locations.
The first RewriteCond might not be required, depends on what else is configured inside your http server.
UPDATE:
For what you ask in your first comment to this question I'd suggest something along these lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?3dprintfo\.com$
RewriteCond %{QUERY_STRING} ^id=([^&]+)(?:&|$)
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^/?doku\.php$ /%1.html [R=301,QSD,L]
This variant additionally checks if a file named for example 3dproject2.html actually exists before rewriting to it.

How to redirect a new page (wrong URL) to another page?

I'm new at programming. We have an office project, the website's URL is www.project.com.ph (sample name), this is already a live website from the client. But the released printouts have the instructions for the users to go to www.project.com/ph which is wrong and we can't reprint the material since it already reached plenty of event places.
Now the problem is, we need to redirect to www.project.com.ph automatically if the users type in the browser's address bar www.project.com/ph. I ask if this is possible without any kind of CMS or Wordpress and how to actually do it? We bought a new domain www.project.com for this. Any kind of help is appreciated.
Try the following near the top of your .htaccess file in the root of www.project.com. This works OK (although marginally less efficient) if both domains are pointing to the same place, since it specifically checks that we are requesting the "wrong" domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?project\.com$ [NC]
RewriteRule ^ph/?(.*) http://www.project.com.ph/$1 [NC,R=302,L]
This will redirect requests for www.project.com/ph (no slash), www.project.com/ph/ (with a trailing slash) and www.project.com/ph/<whatever> to http://www.project.com.ph/<whatever>.
This is a temporary (302) redirect. Change it to a permanent (301) only when you are sure it's working OK.
From kj.'s answer on a similar question, here
In your .htaccess for www.project.com, this should do the trick.
RewriteEngine on
RewriteRule ^(.*)$ http://www.project.com.ph/ [R=permanent,NC,L]
This will redirect any request to project.com to the domain http://www.project.com.ph/
To include the path after the /ph/` you can use this.
RewriteEngine on
# redirect including path after ph/ (e.g. project.com/ph/directory/file.php to project.com.ph/directory/file.php
RewriteRule ^ph/(.*)$ http://www.project.com.ph/$1 [R=permanent,NC,L]
# redirect any other requests to project.com.ph index
RewriteRule ^(.*)$ http://www.project.com.ph/ [R=permanent,NC,L]
You can redirect (301 redirect) the URL using RewritrRule in .htaccess file
RewriteRule "http://www.project.com/ph/(.*)" "http://www.project.com.ph/$1" [L,NC,R=301]

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.

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

301 redirect rule in 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

Resources