htaccess rewrite rules - .htaccess

I have a url structure like this:
\http://www.domain.com/virtual_tours/virtual_name/
\http://www.domain.com/virtual_tours/virtual_name/virtual_tour_name-001.php?format=something
\http://www.domain.com/virtual_tours/virtual_name/virtual_tour_name-002.php?format=something
and I need to map those to the new urls that will look like this:
\http://www.domain.com/virtual-tours/virtual-name.php
\http://www.domain.com/virtual-tours/virtual-name/virtual-tour-name-001.php
\http://www.domain.com/virtual-tours/virtual-name/virtual-tour-name-002.php
Can someone help me with the condition and rule for this?

I don't think that is good idea to perform something like str-replace inside .htaccess.
Much more better and flexible method is to rewrite all URL you need to the php script and perform the REQUEST_URI parsing and URL routing inside via PHP code.

RewriteEngine On
RedirectMatch permanent ^/(virtual_tours)/(virtual_name)$ http://www.domain.com/virtual-tours/virtual-name.php
RedirectMatch permanent ^/(virtual_tours)/(virtual_name)/virtual_tour_name-(.*)\?.*$ http://www.domain.com/virtual-tours/virtual-tour-name-$1.php

Related

.htaccess rewrite only when there is a slash in the URL

In my root directory I have files but I want the rewrite to work only if there is a slash.
I have urls at the moment as www.mysite.com/index.php?id=10&id1=234. I want instead to have urls like www.mysite.com/234 that rewrite to passthrough.php?id=234.
I will use php to redirect. The important point is the rewrite should only happen if there is a slash so it does not conflict with other stuff.
Thanks in advance!
I have sorted this out now thanks by simply looking at the rewrite template and all works fine. It can be a bit confusing when you first use such things!

Setup htaccess not to redirect when hit from main site

I'm not even sure how to ask this correctly so if I am duplicating a question I apologize. How do I use my htaccess file to only redirect when someone is coming in on something other than the main site name?
Example:
I do not want redirect on www.examplesite.com
I do want to redirect on www.examplesite.com/page.php
I think this is what you're looking for:
Perform a redirection with .htaccess
The easiest and simplest way of redirecting with .htaccess is to use the Apache module mod_alias and its command Redirect. Here’s is how to make a temporary redirection with htaccess:
Redirect /page.php http://www.examplesite.com/go_to_this_page.php
Is this along the lines of what you're asking? If so, I hope it can help.
The Structure : redirect accessed-file URL-to-go-to
The code :
Redirect 301 / http://www.examplesite.com/page.php

.htaccess query rewrite rules

I am trying to make it for when a client loads the website:
http://www.example.com/docs/example
To actually load the address:
http://www.example.com/docs.php?doc=example
But not have it redirect. I have had mixed success with this, but it seems that there is not clear documentation on how this specific rewrite rule is made. If I actually knew regular expressions, then it would make this a lot easier i understand, but I am seeking some help on how to perform this.
Thanks so much!
In the htaccess file in your document root, add these rules:
RewriteEngine On
RewriteRule ^/?docs/(.*)$ /docs.php?doc=$1 [L]
Don't use the http://hostname/ or the R flag and it shouldn't redirect.

Redirect every incoming request to specific path preserving file and query?

As I'm not strong with apache could someone point me in right direction with this?
I currently have urls like this
www.domain.com/public/my_file.php?query=thing&another=thing
What I'm looking to do is to rewrite my code so i it don't use /public/ part anymore, but after that i still need to support all crawlers and old urls people are linking to.
So how would i do 301 redirect preserving everything that comes after public/ part?
(Example) Need to redirect something like this
www.domain.com/public/my_file.php?query=thing&another=thing
into this
www.domain.com/my_file.php?query=thing&another=thing
with 301 redirect.
Thnaks.
Redirect 301 /public/my_file.php /my_file.php
The query string gets passed along by default.
EDIT:
To redirect everything in the public folder, use this:
RedirectMatch 301 /public/(.*) /$1

Simple url rewrite

I'm trying to rewrite part of an url as I've changed a CMS and still want Google to find my articles.
I have:
www.mywebsite.com/vision
www.mywebsite.com/vision/40/some-article-name
and want to rename them:
www.mywebsite.com/news
www.mywebsite.com/news/40/some-article-name
Any hints as to the re-write rules or where I can look? I'd like to change the rules in my .htaccess file.
# Activate Rewrite Engine
RewriteEngine On
# redirect /vision to /news
RewriteRule ^vision$ http://www.mywebsite.com/news [R=301,NC]
# redirect /vision/bla-bla to /news/bla-bla
RewriteRule ^vision/(.*)$ http://www.mywebsite.com/news/$1 [R=301,NC,QSA]
In theory (and practically) these 2 rewrite rules can be combined, but then if you have URL that starts with "vision" (like this, for example: /visions/hurray) then such rule may redirect wrong URLs. Therefore I have done it via 2 rules which is much safer.
Try: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
or: http://httpd.apache.org/docs/2.2/mod/mod_substitute.html if you want to change links in the html content returned to the browser.
Here is an example of how I might do the rewrite I think you're after...
RewriteRule ^(.)/vision/(.)$ $1/news/$2
This may be to broad of a rewrite scope in which case this may be better...
RewriteRule http://www.mywebsite.com/vision/(.*)$ http://www.mywebsite.com/news/$1
Also learning the basics of regex will be a needed skill for doing any complex rewriting IMO.
Hope that helps.

Resources