Rewrite URLs Using .htaccess - apache

Referring to the article here I created my .htaccess file with the following lines
RewriteEngine on
RewriteRule ^/product/([0-9]+)/(.*)$ /index.php?route=product&product=$1 [L]
but this doesn't work and I get a 404 error.
I want URLs like
http://localhost/product/12/some-random-text.html
to be redirected to
http://localhost/index.php?route=product&product=12
I am using GoDaddy's Linux hosting

Try changing your RewriteRule to:
RewriteRule ^product/([0-9]+)/(.*)$ /index.php?route=product&product=$1 [QSA,L]

Related

Disguising URL by rewriting htaccess

I have a site built for a client located on my server (ie http://www.myserver.com/clientsite). Can I disguise the URL by modifying the .htaccess file on my client's host (http://www.clientsite.com) to read as his domain, but display my content and keep the subdirectories in tact?
So: http://www.myserver.com/clientsite would read http://www.clientsite.com
and http://www.myserver.com/clientsite/about would read http://www.clientsite.com/about
I tried the following, but it was directing me to a 404 error.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^clientsite.com
RewriteRule ^(.*) http://www.myserver.com/clientsite/$1 [P]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?clientsite.com
RewriteRule ^(.*) http://www.myserver.com/clientsite/$1 [P]
Since you mentioned that your client's site is http://www.clientsite.com and Rewrite condition was applied only to clientsite.com , I changed it to support both with and without www.
If it doesn't work, put your rewrite log and I'll check that.

.htaccess in subfolder doesn't work

I'm hosting my site on Godaddy server.
On the server I have 5 sites, each site on individual folder.
for example: "webroot/site1"
I directed specific domain to the specific folder on the server "/site1"
The problem:
I tried to make a RewriteRule with .htaccess in the subfolder but it keeps telling me 404 not found.
This is the .htaccess:
RewriteEngine On
this works fine:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
this redirect to 404 page:
#rewirite categories only
RewriteRule ^category/([0-9]+)/.*$ /category.php?c_id=$1 [L,QSA]
this url works perfect:
www.example.com/category.php?id=1
this url doesn't work and redirect to 404 page
www.example.com/category/1/blalba
More details:
The other 4 folders are word press sites.
In the webroot there is another wordpress site.
In the webroot there is an empty htaccess file.
I already tried to do
I already tried to do:
RewriteRule ^category/([0-9]+)/.*$ /site1/category.php?c_id=$1 [L,QSA]
EDIT:
I don't have category folder is just for the URL I want that it will redirect me to category.php with the id parameter
This is the error I get:
Not Found
The requested URL /site1/category/1/area-rugs was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
The RewriteRule will need to be in the htaccess file in the root folder of the website.
E.g. if the directory structure is something like this:
/webroot/site1.com/
/webroot/site1.com/.htaccess
/webroot/site1.com/index.php
/webroot/site1.com/category.php
I'd have this code in the .htaccess file in site1.com:
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^site1.com
RewriteRule ^(.*)$ http://www.site1.com/$1 [R=301]
# Handle categories
RewriteRule ^category/([0-9]+) /category.php?c_id=$1 [L,QSA]

.htaccess mod_rewrite redirect without query string variables not working

I'm having a problem with one of my rewrite rules. I would like to redirect all of the following URL's to another URL without the query string.
/gallery/products.aspx?C=9&SC=&ID=428&P=10
/gallery/products.aspx?C=2&SC=2&ID=128&P=1
/gallery/products.aspx?ID=147&C=2&SC=&P=7
/gallery/products.aspx?ID=1337&C=15&SC=&P=1
/gallery/products.aspx?ID=1532&C=3&SC=&P=2
/gallery/products.aspx?C=9&SC=&ID=1489&P=1
/gallery/products.aspx?C=7&SC=&ID=100&P=2
/gallery/products.aspx?C=2
/gallery/products.aspx?ID=1328&C=14&SC=11&P=17
/gallery/products.aspx?C=1&SC=&ID=767&P=3
/gallery/products.aspx?ID=1270&C=1&SC=&P=26
and I have this in my .htaccess file
RewriteRule ^gallery/products.aspx http://www.domain.com/category/? [L,R=301]
but it's not working. I checked it in a .htaccess simulator and it found the rule then redirected, but when I upload to my server, it doesn't redirect. I've also tried some other rules with no luck
I was finally able to make this work with the following:
RewriteCond %{HTTP_HOST} www.domain.com [NC]
RewriteRule products.aspx http://www.domain.com/category? [L,R=301]

url rewrites dynamic urls redirects - Magento

I'm looking for htaccess lines that do the following
Redirect old existing urls to new url (301)
Example
www.example.com/categorya/categoryb/product.htm
TO
www.example.com/product.htm
There can be more category parts, or less, it all has to go to /product.htm (Magento).
Who can help?
Try putting these rules in the htaccess file in your document root, preferably above any rules that you may already have there:
RewriteEngine On
RewriteRule ^(.+)/product.htm$ /product.htm [L,R=301]
Try:
RewriteEngine On
RewriteRule ^[^/]+/[^/]+/([^.]+)\.htm$ /$1.htm [L,R=301]

Apache .htaccess: Serving .css files from separate domain?

I'm trying to serve CSS files for my site from a separate domain.
I've got the following entry in my site's .htaccess (in the document root):
RewriteRule ^templates/*\.css$ http://css.mysite.com/templates/$1 [R=301,L]
With the intention of simply matching:
http://www.mysite.com/templates/default/styles/main.css
… and sending an HTTP 301 redirect to:
http://css.mysite.com/templates/default/styles/main.css
But what's actually getting called is:
http://css.mysite.com/templates/.css
(I'd like to duplicate the above for *.js and serve them from js.mysite.com and all png & jpgs and serve them from img.mysite.com.)
Any insights would be fantastic. I've got to admit, htaccess rewrite rules always seem to elude me.
Try this and let me know:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^css\.
RewriteRule ^templates/(.*)\.css$ http://css.mysite.com/templates/$1.css [R=301,L]
or a more "dynamic way"
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)\.(js|css)$ http://$2.mysite.com/templates/$1.$2 [R=301,L]
RewriteRule ^templates/(.*)\.(jpg|gif|swf|jpeg|png)$ http://imgs.mysite.com/templates/$1.$2 [R=301,L]
This one will check for JS and CSS and use the subdomain based on the file extension: so .js will go to js.mysite.com and .css will go to css.mysite.com and same for images but these goes to imgs.mysite.com instead.
or be even more generic:
RewriteCond %{HTTP_HOST} ^(www\.)?mysite
RewriteRule ^templates/(.*)$ http://media.mysite.com/templates/$1 [R=301,L]
You redirecting everything that start with templates