Redirect the URL using .htaccess file - .htaccess

I have a page showing the products with the hyperlink for it as
www.domainname.com/productname
now my client needs to add store and needs the URL to show as
www.domainname.com/store/productname
I have done it via code and now when I click on it for a detail page, its still redirecting to
www.domainname.com/productname
but need to be
www.domainname.com/store/productname
tried with this:
RewriteRule ^store/?$ domianname.com/?$ [NC,L]
in .htaccess file, not sure whether I'm on page
Can any one tell me how to do it via .htaccess file.

Your RewriteRule is backwards: you need the path you're matching first, then the path you're redirecting to. Try this:
RewriteRule !^/store/(.*) /store/$1 [NC,L]
Also, you don't actually need mod_rewrite to do this. You could try mod_redirect, which is simpler and easier to understand:
RedirectMatch !^/store/ /store/
(N.B. I haven't tried either of these, so I'm not 100% certain they do what you want.)

Related

htaccess rule to redirect?

I have a link like www.example.com/hello-1/hey-2, now what I want to do is that I want to redirect to this link from www.example.com/hello-1/hey. How to write a htaccess rule for this. I have tried searching on it, tried the same way to reduce index.php but that didn't work.Thank you.
See, basically I want people to be redirected when the type www.example.com/hello-1/hey this was my old link. I want them to be redirected to www.example.com/hello-1/hey-2, where this 2 is important as it's passing a parameter for me.
Basically, it's possible to do so;
RewriteEngine On
RewriteRule ^hello-1/hey$ hello-1/hey-2 [R=301,L]

URL rewrite conditions in htaccess file

I need some help with .htaccess. I've been using URL rewrites, but not conditions, and I'm pretty sure I need them seeing as I can't get this working.
The URL structure I need is -> forum/catnamehere (which shows the page called catnamehere)
and child structure -> forum/catnamehere/fornamehere (which shows the child page of the cat)
However, somehow I fail to do this, making me believe I need conditions.
You should be able to do this with two rules.
Put your .htaccess file in the forum directory, and add the following:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1 [QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)(/?)$ complexForumPath.php?catName=$1&subPage=$2 [QSA]
The first rule will grab stuff like forum/abc123_
And the second rule will grab forum/abc123_/abc123_

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

htaccess rewrite rule 'page numbers' problem

I've added a .htaccess file to my root folder for the purposes of rewriting dynamic URLs into static ones. This seems to have been done successfully but I am having problems with page numbers.
For example, if you were to visit a static page /widgets, the first page of the products is fine....but subsequent pages show up as /products.php?cat=27&pg=2 etc. What I want is for subsequent pages to be in the form of /widgets-pg2 or /widgets?pg=2.
Below is my rewrite rule that I used for the initial category page:-
RewriteRule ^widgets$ products.php?cat=27
If any of you experts can help with this, it would be much appreciated.
Are you expecting the cat to change as well? You'd need to account for that in your URL as well:
e.g. www.site.com/widgets/27/2 could be rewritten as:
RewriteRule ^widgets/([0-9]+)/([0-9]+)$ products.php?cat=$1&pg=$2
If widgets will always be cat 27 then you can change it to:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
which is query string append
Try
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
After that, go here :)
To allow a query string after your rewritten URL use the [QSA] flag:
RewriteRule ^widgets$ products.php?cat=27 [QSA]
A link would than be:
http://example.org/widgets?pg=167&perpage=100&tralala=Hi!
I tried the following but it resulted in a '404 Not Found' error when I go to /widgets:-
RewriteRule ^widgets-pg(.+)$ products.php?cat=27&pg=$1
And I tried the following:-
RewriteRule ^widgets$ products.php?cat=27 [QSA]
This worked correctly but to go to page two of the widgets page, I need to type in the browser:-
/widgets?pg=2
But the actual 'page 2' link still leads to:-
products.php?cat=27&pg=2
So apart from a rewrite rule....maybe I need a separate redirection rule? Or maybe need to change the page number links. But since these are dynamically generated, I'm not sure how to do this.
The following is the PHP code for the page:- http://freetexthost.com/3ubiydspzm

.htaccess and url

Is it possible somehow using mod_rewrite change the url from http://www.mywebsite.com/company/123/reviews to http://www.mywebsite.com/company-123/reivews?
It's not a redirect. The problem is that the real path is the first one and I need my browser to display the second path. So when the user goes to company-123/reviews the content of the page is displayed from company/123/reviews.
Thank you.
RewriteRule ^/([a-z]*?)-([0-9]*?)/([a-z]*?)$ /$1/$2/$3
I think this will work, the regex does it's job atleast.
Use this rule to rewrite the former URL path to the latter one:
RewriteEngine on
RewriteRule ^([^/]+)-([0-9]+)/([^/]+)$ $1/$2/$3 [L]
But you already need the former URLs in your documents to get this rewriting work.

Resources