Use htaccess to mask few urls - .htaccess

How can I use htaccess to redirect a few static pages and keep the same url?
Example :
Redirect www.mydomain.com/url-that-remains.php to www.mydomain.com/page-number-23
My current htaccess file looks like that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This code should work:
RewriteEngine On
RewriteRule ^url-that-remains\.php$ /page-number-23 [L,NC]

Try like this
RewriteEngine On
RewriteRule /somestaticpageonyourwebsite.html /yournewpage.html [R=302]
EDIT:
I don't know why the above doesn't work. This should work.
RedirectPermanent /somestaticpageonyourwebsite.html /yournewpage.html

Related

htaccess query string redirect not working

I am trying to redirect http://example.com/for-sale/?property=1234 to http://example.com/for-sale/property-name/ in .htaccess, using RewriteCond and RewriteRule. This is what I have so far:
RewriteCond %{QUERY_STRING} ^(property=1234)
RewriteRule ^$ /for-sale/property-name/? [R=301,L]
This does not work. I can redirect ?property=1234 to for-sale/property-name/ but how do I get it to work with /for-sale/ in the un-redirected URL? I've tried various combinations of for-sale in the RewriteRule but without success.
Thanks.
All my links are now broken so I would really appreciate some advice. Thanks.
This is a wordpress site, so the first mistake was to do the rewrites after wordpress does its rewrites. Second mistake was in the RewriteRule. Here is the working code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} property=1234$
RewriteRule ^for-sale/?$ /for-sale/property-name/? [R=301,L]
... my other rewrites
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This works. Hope it helps someone else out.

Rewrite in htaccess not working

I have a page locally that is /shop but I want it to redirect to /shop/designs
I tried to do it with .htaccess but nothing seems to be working.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/shop/.*$ /shop/designs/ [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any ideas where I am going wrong?
You were almost there, but in htaccess, you cannot use / at the beginning of the path regex.
Change your rule like so:
RewriteEngine On
RewriteRule ^shop$ shop/designs [R=301,L]

How to redirect using .htaccess?

I need my site to redirect from this url:
mysite.com/old/dir/param1/param2/...
to
mysite.com/dir/param1/param2/...
I need to keep the params, just remove the "old" in the url.
How can I add this rule to my .htaccess?
Thanks!!!
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]
</IfModule>
The right rule for it was
and I had to place it before all the other rules. So my .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old/dir/(.*) /dir/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
Thanks anyway! :)
You could just write it like this.
Rediret 301 mysite.com/old/dir/param1/param2/(.*) mysite.com/dir/param1/param2/$1
If you're planning to redirect the entire directory of old/dir to dir/, then try this:
Redirect 301 mysite.com/old/dir/(.*) mysite.com/dir/$1
The next option you could try is, Url Rewriting.
Assuming that you have set your Rewrite Engine on and have set the Rewrite Base, add these lines:
RewriteRule ^old/dir/(.*) mysite.com/dir/$1 [R=301,L]

how rewrite url from subfolder at root

I have more link in Google es:
http://www.mysite.com/blog/23-08-2012/example.html
http://www.mysite.com/blog/more/
http://www.mysite.com/blog/test/example.html
how to rewrite the url by removing the word "blog" in htaccess es:
http://www.mysite.com/23-08-2012/example.html
http://www.mysite.com/more/
http://www.mysite.com/test/example.html
EDIT
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RE-EDIT solution
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RedirectMatch 301 ^/blog/(.*)$ /$1
it is important to put this line at the end, not the beginning
RedirectMatch 301 ^/blog/(.*)$ /$1
You only need mod_alias to do this:
RedirectMatch 301 ^/blog/(.*)$ /$1
You can use that in your vhost config or the htaccess file in your document root.
Surprise that you don't have mod_alias, it's pretty much loaded on default. But since you're already using mod_rewrite:
If this is what you have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Then above the RewriteBase, add this:
RewriteRule ^blog/(.*)$ /$1 [L,R=301]
Just a minute or two to read Apache's Rewrite Rule documentation would've provided you with answer. Here's an outline to what you can do:
Options +FollowSymLinks
RewriteEngine On
RewriteCond ^blog/
RewriteRule ^blog/(.*)$ http://mysite.com/$1 [R=301,L]

rewrite/redirect specific word from url .htaccess

I have couple of urls which is generated by wordpress/php
http://mydomain.com/news-2/newsarticle
http://mydomain.com/products/category-2
http://mydomain.com/products/category/products-2
how can I rewrite/redirect any url with -2 to the one without? The result should be
http://mydomain.com/news/newsarticle
http://mydomain.com/products/category
http://mydomain.com/products/category/products
This is what I have so far
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks
Add this to you htaccess :
RewriteRule ^(.*)-2(.*)$ /$1$2 [NC,L,R=301]

Resources