htaccess rule to redirect? - .htaccess

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]

Related

Redirect the URL using .htaccess file

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.)

URL rename not showing on Browser

I change my URL name using mod write. But my URL doesn't show the changes on browser.
This is how it looks from before and after
www.mydomain.com/toy/image.php
to this
www.mydomain.com/toy/xbox
How can I make this: www.mydomain.com/toy/xbox appear on the browser
Another words on my website it should appear www.mydomain.com/toy/xbox instead of this
www.mydomain.com/toy/image.php
This is my code:
RewriteEngine On
RewriteRule ^toy/xbox$ /toy/image.php* [L,R]
Can someone to explain to me how it works. Did I missed a step? Do I need to used PHP?
If I did make a mistake, correct me so I can learn from my mistakes. I try to google this but I couldn't find what I need to do
Any links or explanations would be appreciated. Thanks.
For it to "show the URL," you need it to do a 301/302 redirect, with a location header. All you have to do is end your RewriteRule line with [L,R=301]
You must perform a redirect using the R flag instead of just a rewrite.
RewriteRule ... ... [R]

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

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