Using htaccess to redirect subfolder and variable - .htaccess

I've written the following code so that mysite.com/ticket/{n} where {n} represents a number redirects to othersite.com/ticket/{n}.
So far not having much luck getting it working.
RewriteEngine on
RewriteRule ^/ticket/(\d+)/?$ http://othersite.com/ticket/$1 [L]

Related

htaccess rewrite for known URL and query string

Have been struggling with a 301 redirect issue and cannot see the wood for the trees. Really hope someone can assist.
I have over 400 old urls that we need to redirect to a new urls. I have however modified the htaccess to create a clean url on the new site and am not sure if this is causing an issue or if I am doing something else wrong.
To clean urls in the new site I have added the following to the htaccess fle:
RewriteEngine On
RewriteRule ^([0-9]+) category?cat=$1 [NC,L,QSA]
This is so that instead of www.mysite/category?cat=1 I can have www.mysite/1 and-add-other-information
I am now trying to do the redirects for the 400 urls such as:
RewriteRule ^category?cat=1 1/and-add-other-information [R=301,NC,L,QSA]
RewriteRule ^category?cat=2 1/and-different-information [R=301,NC,L,QSA].
I know the L flag makes a difference and have tried this with and without.
I hope this makes sense.
QueryString isn't part of match in pattern of a RewriteRule.
You need to use RewriteCond directive and match against %{QUERY_STRING} variable like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=1$
RewriteRule ^category$ /and-add-other-information? [R=301,NC,L,QSA]

RewriteRule redirects to unexpected page

I am trying to change my url through .htaccess in the following way
Original URL: http://www.example.com/latest-news.php?id=2/topic=testing
Rewritten URL:http://www.example.com/2/testing
Rule for .htaccess
RewriteRule ^([^/]*)/([^/]*)$ /latest-news.php?id=$1&topic=$2 [L]
This is working fine but the other files which are existing in a folder are not opening. The url is opening as www.example.com/testing/foo.php but content of the page is of http://www.example.com/2/testing
Can you try with a similar rule like the one bellow?
RewriteRule ^([0-9]+)/([a-zA-Z])$ /latest-news.php?id=$1&topic=$2 [L]
Can't really test it on my server but you can adapt it to suit your needs
RewriteEngine On
RewriteRule ^([0-9]+)/([^/]*)$ /latest-news.php?id=$1&url=$2 [L]
Since second part consists characters and digits So I modify the answer of stoica. Thanks #stoica

htaccess redirect causes loop

We have links that look like this that are located on websites that point back to us:
http://www.example.com/campaign
On our website we have a javascript that changes the phone numbers whenever the URL string shows the phone number parameter.
Example: http://www.example.com/campaign?tfid=8885551212 (this would change all the phone numbers to 888-555-1212)
I've tried to set a 301 redirect in htaccess to append the "?tfid=8885551212" string such as this:
Redirect 301 /campaign http://www.example.com/campaign?tfid=8885551212
The problem is that this causes a redirect loop, so I'm wondering if there's a way to do this so it won't cause the loop?
I found the solution by moving a ReWrite condition to the top of the htaccess file before anything else could interfere with it:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^campaign1$ http://www.example.com/campaign1?tfid=8885551212 [L,NE,R=301]

Rewrite URL and permanently redirect to new URL

I've just spent the last 2 hours trawling through Google and the Stackoverflow archives to see if I could find an answer to my question and instead of finding nothing, I've found too much! So unfortunately I'm having to add to the mountain of 301 redirect questions. Sorry. And thanks for taking a look at this one ;)
Basically, I've got a blog for which I'm looking to simplify the URL. Currently the URLs look like this:
http://tempertemper.net/post.php?s=2012-05-30-freeagent
I'd like them to look like this:
http://tempertemper.net/2012-05-30-freeagent
I've tried adding RewriteRule /(.*)$ /post.php?s=$1 to my .htaccess file but it's not having it.
The full file currently looks like this:
<ifModule mod_rewrite.c>
DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.tempertemper\.net$
RewriteCond %{REQUEST_URI} !cron.php
RewriteRule ^(.*)$ http://tempertemper.net/$1 [R=301,L]
</ifModule>
I've tried adding this after the canonicalisation bit that redirects www. to non www.:
RewriteRule ^([a-zA-Z0-9_-]+)$ post.php?s=$1
It sort of works. I can then link to http://tempertemper.net/2012-05-30-freeagent but the problem is, http://tempertemper.net/post.php?s=2012-05-30-freeagent still exists. This is more than likely bad for seo as I guess I'll have duplicate content, in the eyes of the search engines. I've tried putting a [R=301] on the end of the new line but it stops the new link working. Also tried putting the ,L in the square brackets and removing it from the canonicalisation command, but no joy (L is for last command, isn't it…?).
Basically, I'm after all URLs that generate or have generated with post.php?s= in them to permanently redirect to the ones without post.php?s= so that any links that come in to those pages already are redirected and any links in the future will direct straight to the new-look URL.
Hope that makes sense…
Thanks,
Martin :)
Apache actually fixes local paths => proper fully qualified names (my original assumption), but I see that your file actualy checks for the wrong files. Using your supplied file I think this will work as the file contents:
DirectoryIndex index.php index.html
ErrorDocument 404 http://tempertemper.net/error.php
RewriteEngine On
# don't redirect post.php to itself
RewriteCond %{REQUEST_URI} !post.php
# don't redirect error.php to post.php
RewriteCond %{REQUEST_URI} !error.php
RewriteRule ^(.*)$ "/post.php?$1" [R=301,L]

htaccess URL Rewrite for products doesn't redirect me

There is plenty of information out there but nothing I've read on the interwebz has given me an answer as to why my htaccess is not working.
I cannot determine why my rule isn't rewriting the URL as I thought it would. I have the following url:
domain.com/Book/bookpage/index.php?bookID=123&bookName=foo_bar
I would like to change it so that when someone hits that URL, it shows like:
domain.com/Book/123/foo_bar
I started off trying to get it to work using just the Book ID and haven't even gotten that to work.
This is what I have thus far:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2
However, after placing that htaccess in the root of the site and going to the URL:
domain.com/Book/bookpage/index.php?bookID=123
The URL in the address bar remains the same.
try this:
RewriteEngine On
RewriteBase /
# this rewrite domain.com/Book/123 or domain.com/Book/123/
RewriteRule ^Book/([0-9]+)/?$ /Book/bookpage/index.php?bookID=$2 [L,NC,QSA]
# this rewrite domain.com/Book/123/title or domain.com/Book/123/title/
RewriteRule ^Book/([0-9]+)/([a-z0-9\-_]+)/?$ /Book/bookpage/index.php?bookID=$1&bookName=$2 [L,NC,QSA]
Try adding [L,R=301] at the end of the line:
RewriteRule ^Book/([0-9]+)$ /Book/bookpage/index.php?bookID=$2 [L,R=301]

Resources